本帖最后由 武嘉豪 于 2013-9-19 12:26 编辑
- package PhoxyDemo;
- import java.lang.reflect.*;
- import java.util.*;
- public class ProxyDemo
- {
- public static void main(String[]args)
- {
- ArrayList al=new ArrayList();
- //List haha=(List) Proxy.newProxyInstance(ArrayList.class.getClassLoader(), ArrayList.class.getInterfaces(), new handler(al));
- //被注释掉的可以运行,下面的这句就会报错
- ArrayList haha=(ArrayList) Proxy.newProxyInstance(ArrayList.class.getClassLoader(), ArrayList.class.getInterfaces(), new handler(al));
- haha.add("哈哈哈");
- System.out.println(al);
- }
- }
- class handler implements InvocationHandler
- {
- private ArrayList haha;
- handler(Object obj)
- {
- this.haha=(ArrayList)obj;
- }
- public Object invoke(Object arg0, Method arg1, Object[] arg2)throws Throwable
- {
- arg1.invoke(haha, arg2[0]);
- return true;
- }
- }
复制代码 |