黑马程序员技术交流社区

标题: 代理类的创建问题 [打印本页]

作者: 清风有意    时间: 2014-4-9 16:11
标题: 代理类的创建问题
本帖最后由 清风有意 于 2014-4-9 16:12 编辑

代码如下
public class Test2 {

        public static void main(String[] args)throws Exception {
                ArrayList proxy=(ArrayList)Proxy.newProxyInstance(
                                ArrayList.class.getClassLoader(),
                                new Class[]{Serializable.class,Cloneable.class,Iterable.class,Collection.class,List.class,RandomAccess.class},
                                new InvocationHandler() {
                                       
                                        @Override
                                        public Object invoke(Object proxy, Method method, Object[] args)
                                                        throws Throwable {
                                                ArrayList target=new ArrayList();
                                                long beginTime=System.currentTimeMillis();
                                                Object retVal=method.invoke(target, args);
                                                Thread.sleep(1000);
                                                long endTime=System.currentTimeMillis();
                                                System.out.println(method.getName()+"Running "+(endTime-beginTime));
                                                
                                                return retVal;
                                        }
                                });
        proxy.add("abc");
        proxy.add("def");
        proxy.add("ghi");
        
        }
}

出现的问题是$Proxy0 cannot be cast to java.util.ArrayList。
哪里错了
作者: ╰青青子佩ˊゝ    时间: 2014-4-9 16:43
1个错误,2个提示,希望能帮到你,如下面代码所示。
  1. public class Test2{
  2.        
  3.         public static void main(String[] args)throws Exception{
  4.                 //1.把ArrayList改成List或是Collection都行.因为JVM生成的动态类必须实现一个或多个接口。而ArrayList不是接口
  5.                 List proxy=(List)Proxy.newProxyInstance(
  6.                  ArrayList.class.getClassLoader(),
  7.              //  new Class[]{Serializable.class,Cloneable.class,Iterable.class,Collection.class,List.class,RandomAccess.class},
  8.                  //2.你上一行代码可以简化成下面这行代码
  9.                  ArrayList.class.getInterfaces(),
  10.                  new InvocationHandler() {
  11.                         //3.把这行代码从下面提上来会更好,在在invoke方法里面的话,就是产生了3个不同的ArrayList对象.
  12.                                  ArrayList target=new ArrayList();
  13.                          @Override
  14.                          public Object invoke(Object proxy, Method method, Object[] args)
  15.                                          throws Throwable {
  16.                                         
  17.                                  
  18.                                  long beginTime=System.currentTimeMillis();
  19.                                  Object retVal=method.invoke(target, args);
  20.                                  Thread.sleep(1000);
  21.                                  long endTime=System.currentTimeMillis();
  22.                                  System.out.println(method.getName()+"Running "+(endTime-beginTime));
  23.                                  
  24.                                  return retVal;
  25.                          }
  26.                  });
  27.                                 proxy.add("abc");
  28.                                 proxy.add("def");
  29.                                 proxy.add("ghi");
  30.                
  31.         }
  32.        
  33. }       
复制代码

作者: 清风有意    时间: 2014-4-10 14:59
╰青青子佩ˊゝ 发表于 2014-4-9 16:43
1个错误,2个提示,希望能帮到你,如下面代码所示。

谢谢l  很有用




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