- 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());
-
-
-
- }
-
-
- }
复制代码 |