黑马程序员技术交流社区

标题: 咨询个关于代理的问题 [打印本页]

作者: 奋发吧小白    时间: 2014-9-18 09:26
标题: 咨询个关于代理的问题
早晨起床写了个代理,但是在用迭代器取出元素时遇到一个问题!就是不明白 请教大神们帮忙解答下:我把代码贴上来:只是正确的可以运行的代码:

  1. package w2014091801;

  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. import java.util.ArrayList;
  6. import java.util.Collection;


  7. import sun.text.CompactShortArray.Iterator;

  8. public class w20140918proxy {

  9.         /**
  10.          * @param args
  11.          */
  12.         public static void main(String[] args) throws Exception
  13.         {
  14.                 // TODO Auto-generated method stub
  15.                 Class clazzProxy =Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
  16.                 Collection proxy = (Collection) Proxy.newProxyInstance(
  17.                                 Collection.class.getClassLoader(),
  18.                                 new Class[]{Collection.class},
  19.                                 new InvocationHandler()
  20.                                 {
  21.                                         ArrayList<String> target = new ArrayList<String>();
  22.                                         public Object invoke(Object proxy, Method method,
  23.                                                         Object[] args) throws Throwable
  24.                                                         {
  25.                                                         Object retVal = method.invoke(target, args);
  26.                                                         return retVal;
  27.                                                         }
  28.                                        
  29.                                                 }
  30.                                 );
  31.                 proxy.add("nihao");
  32.                 proxy.add("it.cast");
  33.                 proxy.add("wolaile");
  34.                
  35.                 System.out.println(proxy);
  36. <font color="#ff0000">                 java.util.Iterator  it =  proxy.iterator();</font>
  37.                 while(it.hasNext())
  38.                 {
  39.                         System.out.println(it.next());
  40.                 }
  41.         }

  42. }
复制代码
这是出现问题的代码:
  1. package w2014091801;

  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. import java.util.ArrayList;
  6. import java.util.Collection;


  7. import sun.text.CompactShortArray.Iterator;

  8. public class w20140918proxy {

  9.         /**
  10.          * @param args
  11.          */
  12.         public static void main(String[] args) throws Exception
  13.         {
  14.                 // TODO Auto-generated method stub
  15.                 Class clazzProxy =Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
  16.                 Collection proxy = (Collection) Proxy.newProxyInstance(
  17.                                 Collection.class.getClassLoader(),
  18.                                 new Class[]{Collection.class},
  19.                                 new InvocationHandler()
  20.                                 {
  21.                                         ArrayList<String> target = new ArrayList<String>();
  22.                                         public Object invoke(Object proxy, Method method,
  23.                                                         Object[] args) throws Throwable
  24.                                                         {
  25.                                                         Object retVal = method.invoke(target, args);
  26.                                                         return retVal;
  27.                                                         }
  28.                                        
  29.                                                 }
  30.                                 );
  31.                 proxy.add("nihao");
  32.                 proxy.add("it.cast");
  33.                 proxy.add("wolaile");
  34.                
  35.                 System.out.println(proxy);
  36. <font color="#ff0000">                 Iterator  it =  proxy.iterator();</font>
  37.                 while(it.hasNext())
  38.                 {
  39.                         System.out.println(it.next());
  40.                 }
  41.         }

  42. }
复制代码
错误提示:Exception in thread "main" java.lang.Error: Unresolved compilation problem:         Type mismatch: cannot convert from Iterator to CompactShortArray.Iterator

        at w2014091801.w20140918proxy.main(w20140918proxy.java:41)




作者: 奋发吧小白    时间: 2014-9-19 17:56
没人能帮我看看?




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