黑马程序员技术交流社区
标题:
头要炸了,动态代理问题......
[打印本页]
作者:
糖伯貓
时间:
2014-2-14 00:50
标题:
头要炸了,动态代理问题......
class Test9{
public static void main(String[] args) throws Exception {
final ArrayList target = new ArrayList();
List list = (List)Proxy.newProxyInstance(
ArrayList.class.getClassLoader(),
ArrayList.class.getInterfaces(),
new InvocationHandler(){
public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
Long beginTime = System.currentTimeMillis();
Thread.sleep(5);
Object result = method.invoke(target,args);
Long endTime = System.currentTimeMillis();
System.out.println(method.getName()+"runtime is:"+(endTime-beginTime));
return result;
}});
list.add("youxiang");
}
}
复制代码
总是报错 对 Proxy 的引用不明确 哪里不明确了 蛋碎了;
作者:
itpower
时间:
2014-2-14 08:55
class Test9{
public static void main(String[] args) throws Exception {
final ArrayList target = new ArrayList();
List list = (List)Proxy.newProxyInstance(
ArrayList.class.getClassLoader(),
target.getInterfaces(),
new InvocationHandler(){
public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
Long beginTime = System.currentTimeMillis();
Thread.sleep(5);
Object result = method.invoke(target,args);
Long endTime = System.currentTimeMillis();
System.out.println(method<div class="blockcode"><blockquote>.getNa
复制代码
me()+"runtime is:"+(endTime-beginTime));
return result;
}});
list.add("youxiang");
}
}
List list = (List)Proxy.newProxyInstance(
ArrayList.class.getClassLoader(),
ArrayList.class.getInterfaces(),
复制代码
这里不对,ArrayList.class.getInterfaces(),,楼主可以创建个对象穿进去,用对象.getInterfaces(),就可以了
作者:
糖伯貓
时间:
2014-2-14 10:32
itpower 发表于 2014-2-14 08:55
me()+"runtime is:"+(endTime-beginTime));
return res ...
是这样吗new ArrayList().getClass().getInterfaces() 能在详细讲解下吗
class Test10{
public static void main(String[] args) {
final ArrayList target = new ArrayList();
List proxy = (List)Proxy.newProxyInstance(ArrayList.class.getClassLoader(),
ArrayList.class.getInterfaces(),
new InvocationHandler(){
public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
Long beginTime = System.currentTimeMillis();
Thread.sleep(5);
Object result = method.invoke(target,args);
Long endTime = System.currentTimeMillis();
System.out.println(method.getName()+"runtime is:"+(endTime-beginTime));
return result;
}
});
proxy.add("youxiang");
System.out.println(proxy.toString());
}
}
复制代码
这样又编译能通过 我实在看不出来有什么区别
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2