亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于作業的問題

關于作業的代碼實現: InvocationHandler logh=new LogHandler(m); Class

cls2=m.getClass(); Moveable m2=(Moveable)Proxy.newProxyInstance(cls2.getClassLoader(),cls2.getInterfaces(),logh); m2.move(); 請問一下,以上實現方式有啥問題,為什么會報錯?

正在回答

5 回答

System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles",?"true");
Subject?subject?=?(Subject)?Proxy.newProxyInstance(Client.class.getClassLoader(),new?Class[]{Subject.class},new?JdkProxySubject(new?RealSubject()));
subject.hello();
MathSubject?mathSubject?=?new?MathSubject();
InvocationHandler?mathHandler=?new?ProxySubject(mathSubject);
Class<?>?cls?=?mathSubject.getClass();
Subject?mathSubjectResult?=?(Subject)?Proxy.newProxyInstance(Client.class.getClassLoader(),?cls.getInterfaces(),?mathHandler);

InvocationHandler?logmathHandler=?new?LogProxySubject(mathSubjectResult);

Subject??mathResult?=?(Subject)?Proxy.newProxyInstance(Client.class.getClassLoader(),?cls.getInterfaces(),?logmathHandler);

mathResult.hello("wahahaha");
mathResult.request();

// 根據資料,添加了LogProxySubject, ProxySubjec , 替換了JdkProxySubject中的Class換為Interface 接口類Subject,這樣就好了。

0 回復 有任何疑惑可以回復我~

代理類實現的都是moveable接口,所以Class<?> cls = Car.getClass()這一行代碼不用更改,刪掉

Class?cls2=m.getClass();類加載器和接口換回cls就好。


0 回復 有任何疑惑可以回復我~

JDK 動態代理作業可以參考這個

https://github.com/imooc-java/proxy/blob/master/src/test/java/com/imooc/jdkproxy/HandlerTest.java

1 回復 有任何疑惑可以回復我~

有什么錯誤,你貼出來塞

0 回復 有任何疑惑可以回復我~

第二行應該改成 Class<?> clsw = m.getClass();

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

關于作業的問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號