黑马程序员技术交流社区
标题:
动态代理程序中有点小问题(问题在程序中注释)
[打印本页]
作者:
HM王琦
时间:
2013-3-16 19:41
标题:
动态代理程序中有点小问题(问题在程序中注释)
package cn.whap.wq3;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ProxyTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class clazzProxy1=Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
Collection Proxy2=(Collection) Proxy.newProxyInstance(Collection.class.getClassLoader(),
new Class[] {Collection.class},
new InvocationHandler(){
ArrayList target=new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object value=method.invoke(target, args);//此处method.invoke()方法,
//是那里来的,在程序里没有定义method对象。
return value;
}
});
Proxy2.add("zxx");
Proxy2.add("bxd");
Proxy2.add("llx");
Iterator it=Proxy2.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println(Proxy2.size());
}
}
复制代码
作者:
黑马李超
时间:
2013-3-16 20:08
public Object invoke(Object proxy, Method method, Object[] args)
//这里传入了Method对象
throws Throwable {
Object value=method.invoke(target, args);//此处method.invoke()方法,
//是那里来的,在程序里没有定义method对象。
return value;
}
作者:
HM张超
时间:
2013-3-16 23:21
楼上正解
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2