黑马程序员技术交流社区
标题:
动态代理
[打印本页]
作者:
奋发吧小白
时间:
2014-9-16 12:12
标题:
动态代理
需求:为Arraylist集合创建一个动态代理
package interview;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyDemo01 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Class clazzProy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
Collection proxy = (Collection)Proxy.newProxyInstance(
Collection.class.getClassLoader(),
new Class[] {Collection.class},
new InvocationHandler()
{
ArrayList target = new ArrayList();
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object retVal = method.invoke(target, args);
return retVal;
}
}
);
proxy.add("nihi");
proxy.add("sajd");
proxy.add("sad");
System.out.println(proxy);
}
}
复制代码
作者:
Nullifier
时间:
2014-9-16 13:02
刚看完动态代理,在复习一遍
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2