黑马程序员技术交流社区

标题: 头要炸了,动态代理问题...... [打印本页]

作者: 糖伯貓    时间: 2014-2-14 00:50
标题: 头要炸了,动态代理问题......
  1. class Test9{
  2.        
  3.         public static void main(String[] args) throws Exception {
  4.                         final ArrayList target = new ArrayList();
  5.                         List list = (List)Proxy.newProxyInstance(
  6.                                 ArrayList.class.getClassLoader(),
  7.                                 ArrayList.class.getInterfaces(),
  8.                                 new InvocationHandler(){
  9.                                         public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
  10.                                                 Long beginTime = System.currentTimeMillis();
  11.                                                 Thread.sleep(5);
  12.                                                 Object result  = method.invoke(target,args);
  13.                                                 Long endTime   = System.currentTimeMillis();
  14.                                                 System.out.println(method.getName()+"runtime is:"+(endTime-beginTime));
  15.                                                 return result;
  16.                                         }});
  17.                 list.add("youxiang");

  18.         }
  19. }
复制代码


总是报错 对 Proxy 的引用不明确 哪里不明确了 蛋碎了;
作者: itpower    时间: 2014-2-14 08:55
  1. class Test9{
  2.         
  3.         public static void main(String[] args) throws Exception {
  4.                         final ArrayList target = new ArrayList();
  5.                         List list = (List)Proxy.newProxyInstance(
  6.                                 ArrayList.class.getClassLoader(),
  7.                                 target.getInterfaces(),
  8.                                 new InvocationHandler(){
  9.                                         public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
  10.                                                 Long beginTime = System.currentTimeMillis();
  11.                                                 Thread.sleep(5);
  12.                                                 Object result  = method.invoke(target,args);
  13.                                                 Long endTime   = System.currentTimeMillis();
  14.                                                 System.out.println(method<div class="blockcode"><blockquote>.getNa
复制代码

me()+"runtime is:"+(endTime-beginTime));
                                                return result;
                                        }});
                list.add("youxiang");

        }
}
  1. List list = (List)Proxy.newProxyInstance(
  2.                                 ArrayList.class.getClassLoader(),
  3.                                 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() 能在详细讲解下吗
  1. class Test10{
  2.         public static void main(String[] args) {
  3.                 final ArrayList target = new ArrayList();
  4.                 List proxy  = (List)Proxy.newProxyInstance(ArrayList.class.getClassLoader(),
  5.                                                                                          ArrayList.class.getInterfaces(),
  6.                                                                                          new InvocationHandler(){
  7.                                                                                                 public Object invoke(Object obj,Method method,Object[] args) throws Throwable{
  8.                                                                                                                 Long beginTime = System.currentTimeMillis();
  9.                                                                                                                 Thread.sleep(5);
  10.                                                                                                                 Object result  = method.invoke(target,args);
  11.                                                                                                                 Long endTime  = System.currentTimeMillis();
  12.                                                                                                                 System.out.println(method.getName()+"runtime is:"+(endTime-beginTime));
  13.                                                                                                                 return result;
  14.                                                                                                  }
  15.                                                                                          });
  16.                 proxy.add("youxiang");
  17.                 System.out.println(proxy.toString());
  18.         }
  19. }
复制代码

这样又编译能通过 我实在看不出来有什么区别




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2