//创建代理实例对象
Constructor constructor1 = proxy1.getConstructor(InvocationHandler.class);
Collection collection = (Collection) constructor1.newInstance(new InvocationHandler(){
ArrayList alTag = new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
Object retObj = method.invoke(alTag, args);
return retObj;
}
});
//调用方法,原理handler.invoke(this,this.getClass().getMethod("size"),null)
System.out.println(collection.size());
那么这里的this就是collection
我上面写的代码中,Test对象接收了T1对象,Test对象调用size方法,而size方法中又调用了T1对象的invoke方法 |