課程
/后端開發
/Java
/模式的秘密---代理模式
功能疊加怎么做,沒做出來
2019-04-29
源自:模式的秘密---代理模式 2-3
正在回答
EMMMMMMMMMMMM,這就是一種
第一種方法
Car car = new Car();
Class carClass = car.getClass();
InvocationHandler timeHandler = new TimeHandler(car);
Moveable proxy = (Moveable) Proxy.newProxyInstance(carClass.getClassLoader(),?
carClass.getInterfaces(),timeHandler);
InvocationHandler logHandler = new LogHandler(proxy);
Moveable proxy2 =? (Moveable) Proxy.newProxyInstance(proxy.getClass().getClassLoader(),
proxy.getClass().getInterfaces(), logHandler);
proxy2.move();
第二種方法
InvocationHandler h = new TimeHandler(car);
Class<?> cls = car.getClass();
/**
* loader? 類加載器
* interfaces? 實現接口
* h InvocationHandler
*/
Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(),
cls.getInterfaces(), h);
InvocationHandler h2=new LogHandler(m);
Class<?> clh=m.getClass();
Moveable m2=(Moveable)Proxy.newProxyInstance(clh.getClassLoader(),?
clh.getInterfaces(), h2);
m2.move();
其實都差不多。。。
舉報
本節課程將帶你領略Java編程語言中代理模式的奧妙
1 回答試用JDK做動態代理,如何疊加功能?
3 回答JDK動態代理 如何疊加求解
1 回答代理和繼承有什么區別,都是為了拓展業務功能
1 回答只能使用infce.getSimpleName(),不能使用infce.getName()
1 回答老師,能否提供源碼?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-10-30
EMMMMMMMMMMMM,這就是一種
2019-06-06
第一種方法
Car car = new Car();
Class carClass = car.getClass();
InvocationHandler timeHandler = new TimeHandler(car);
Moveable proxy = (Moveable) Proxy.newProxyInstance(carClass.getClassLoader(),?
carClass.getInterfaces(),timeHandler);
InvocationHandler logHandler = new LogHandler(proxy);
Moveable proxy2 =? (Moveable) Proxy.newProxyInstance(proxy.getClass().getClassLoader(),
proxy.getClass().getInterfaces(), logHandler);
proxy2.move();
第二種方法
Car car = new Car();
InvocationHandler h = new TimeHandler(car);
Class<?> cls = car.getClass();
/**
* loader? 類加載器
* interfaces? 實現接口
* h InvocationHandler
*/
Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(),
cls.getInterfaces(), h);
InvocationHandler h2=new LogHandler(m);
Class<?> clh=m.getClass();
Moveable m2=(Moveable)Proxy.newProxyInstance(clh.getClassLoader(),?
clh.getInterfaces(), h2);
m2.move();
其實都差不多。。。