国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

靜態代理 & 動態代理

2019-11-08 00:01:26
字體:
來源:轉載
供稿:網友

最近在學習java代理模式,下面是學習心得

1,對于靜態代理:

interface PRoxyInterFace { void sayHello(); } static class TargetObject implements ProxyInterFace { @Override public void sayHello() { System.out.println("hello world"); } } static class ProxyObject implements ProxyInterFace { private ProxyInterFace object; public ProxyObject(ProxyInterFace obj) { this.object = obj; } @Override public void sayHello() { object.sayHello(); } } public static void main(String[] args) { //代理的目標對象 ProxyInterFace proxyInterFace = new TargetObject(); //代理器 ProxyObject proxyObject = new ProxyObject(proxyInterFace); //調用目標對象的方法, 執行代碼任務 proxyObject.sayHello(); }

2,對于動態代理:

interface ProxyInterFace { void sayHello(); } static class TargetObject implements ProxyInterFace { @Override public void sayHello() { System.out.println("hello world"); } } static class ProxyObject implements InvocationHandler { private Object object; public ProxyObject(Object obj) { this.object = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //do something return method.invoke(object, args); //do something } } public static void main(String[] args) { //代理的目標對象 ProxyInterFace proxyInterFace = new TargetObject(); //代理器 ProxyObject proxyObject = new ProxyObject(proxyInterFace); //轉換成InvocationHandler InvocationHandler handler = proxyObject; //執行代碼任務 Object proxy = Proxy.newProxyInstance(proxyInterFace.getClass().getClassLoader(), proxyInterFace.getClass().getInterfaces(), handler); //轉換成目標對象,調用目標對象的方法 ((ProxyInterFace) proxy).sayHello(); }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 额济纳旗| 商南县| 永平县| 天峻县| 罗田县| 满城县| 和平区| 宝兴县| 库车县| 梅河口市| 青阳县| 松溪县| 滨海县| 高尔夫| 垣曲县| 宁陕县| 昌平区| 永寿县| 化州市| 安徽省| 西吉县| 小金县| 监利县| 邯郸县| 白河县| 涿鹿县| 砀山县| 建湖县| 康定县| 灵寿县| 尚义县| 抚顺市| 海宁市| 万年县| 修水县| 历史| 无为县| 恩平市| 图木舒克市| 霍山县| 汝州市|