首页 > 其他 > 详细

动态代理

时间:2017-10-20 17:23:23      阅读:271      评论:0      收藏:0      [点我收藏+]

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

动态代理类:

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");                                                                                                                                    

}

}

动态代理

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!