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

© 奋发吧小白 高级黑马   /  2014-9-18 09:26  /  802 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

早晨起床写了个代理,但是在用迭代器取出元素时遇到一个问题!就是不明白 请教大神们帮忙解答下:我把代码贴上来:只是正确的可以运行的代码:

  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)



1 个回复

倒序浏览
没人能帮我看看?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马