黑马程序员技术交流社区
标题:
代理类的问题
[打印本页]
作者:
NNERO
时间:
2014-4-22 21:43
标题:
代理类的问题
import java.lang.reflect.*;
import java.util.*;
public class ProxyDemo {
public static void main(String[] args) {
Collection<Integer> proxy = getProxy(Integer.class);
Collection<String> proxy2 = getProxy(String.class);
proxy.add(11);
proxy.add(22);
proxy2.add("hahahahahahahahahaha");
System.out.println(proxy);
System.out.println(proxy2);
}
@SuppressWarnings("unchecked")
public static <E> ArrayList<E> getProxy(Class<E> eType){
ArrayList<E> al = (ArrayList<E>) Proxy.newProxyInstance(
ArrayList.class.getClassLoader(), //这里是什么意思,用类加载器加载代理类吗?
ArrayList.class.getInterfaces(), //这里也不懂
new InvocationHandler() {
ArrayList<E> list = new ArrayList<E>();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object reval = method.invoke(list, args);
return reval;
}
});
return (ArrayList<E>)al;
}
}
复制代码
注释里的2个问题。
还有这个程序运行出错啊,说什么类型转换失败。这个怎么办,求解答!
作者:
osully
时间:
2014-4-22 22:00
老实说 你这个代理 写的真是一塌糊涂
再看一遍张老师的视频吧
作者:
云浮
时间:
2014-4-22 22:42
不过我还是建议他看一下api
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2