标签: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