A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 糖伯貓 中级黑马   /  2014-2-14 00:50  /  1249 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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 的引用不明确 哪里不明确了 蛋碎了;

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1 淡定

查看全部评分

2 个回复

倒序浏览
  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(),就可以了

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1 赞一个!

查看全部评分

回复 使用道具 举报
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. }
复制代码

这样又编译能通过 我实在看不出来有什么区别
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马