标签:cat ble void new 基于 his throwable turn loader
动态代理
Proxy动态代理是基于实现接口的,被代理类实现了某个功能接口,
代理类实现invocationHandler 接口重写invoke(Object proxy, Method method , class 代理类)
用Proxy.newProxyInstance(类加载器, 被代理类实现的接口的集合, invocationhandler 的实现类)来创建代理类对象
//定义歌手的接口
public interface Singer{
void sing();
}
public class Jay implements Singer{
@override
public void sing(){
syso ("唱歌!");
}
}
//定义实现接口的类
public class ProxyJay implements InvocationHandler{
private Object o;
public ProxyJay(){
}
public Object proxy(Object o){
this.o=o;
return Proxy.newProxyInstance(to.getClass().getClassLoader(), o.getClass().getInterfaces(),this);//返回代理类对象
}
@override
public Object invoke(Object proxy , Method method, Object args[]) throws Throwables{
method.invoke(o);
return o;
}
}
public class Text{
main(String[] args){
Singer s=(Singer)new ProxyJay(new Jay());
s.sing();
}
}
标签:cat ble void new 基于 his throwable turn loader
原文地址:http://www.cnblogs.com/liqiang521/p/7487752.html