码迷,mamicode.com
首页 > 其他好文 > 详细

动态代理

时间:2017-10-20 18:30:05      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:throws   一个   player   san   对象   场景   get   class   eth   

动态代理是在实现阶段不用关心代理谁,而在运行阶段蔡指定代理哪一个对象。

动态代理类:

public class GamePlayIH implements InvocationHandler{

Class cls=null;

Object obj=null;

public GamePlayIH(Object _obj){

this.obj=_obj;

}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{

Object result=method.invoke(this.obj,args);

return result;

}

}

 

场景类:

pubilc class Client{

public static void main(String[] args) throws Throwable{

IGamePlayer player=new PlayerCoCo();

InvocationHandler handler=new GamePlayerIH(player);

ClassLoader cl =player.getClass().getClassLoader();

IGamePlayer proxy=(IGamePlayer)Proxy.newProxyInstance(cl,new Class[]{IGamePlayer.class},handler);

proxy.login("zhangsan","pwd");                                                                                                                                    

}

}

动态代理

标签:throws   一个   player   san   对象   场景   get   class   eth   

原文地址:http://www.cnblogs.com/ng1991/p/7700450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!