早晨起床写了个代理,但是在用迭代器取出元素时遇到一个问题!就是不明白 请教大神们帮忙解答下:我把代码贴上来:只是正确的可以运行的代码:
- package w2014091801;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.ArrayList;
- import java.util.Collection;
-
- import sun.text.CompactShortArray.Iterator;
- public class w20140918proxy {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception
- {
- // TODO Auto-generated method stub
- Class clazzProxy =Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- Collection proxy = (Collection) Proxy.newProxyInstance(
- Collection.class.getClassLoader(),
- new Class[]{Collection.class},
- new InvocationHandler()
- {
- ArrayList<String> target = new ArrayList<String>();
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable
- {
- Object retVal = method.invoke(target, args);
- return retVal;
- }
-
- }
- );
- proxy.add("nihao");
- proxy.add("it.cast");
- proxy.add("wolaile");
-
- System.out.println(proxy);
- <font color="#ff0000"> java.util.Iterator it = proxy.iterator();</font>
- while(it.hasNext())
- {
- System.out.println(it.next());
- }
- }
- }
复制代码 这是出现问题的代码:
- package w2014091801;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.ArrayList;
- import java.util.Collection;
-
- import sun.text.CompactShortArray.Iterator;
- public class w20140918proxy {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception
- {
- // TODO Auto-generated method stub
- Class clazzProxy =Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- Collection proxy = (Collection) Proxy.newProxyInstance(
- Collection.class.getClassLoader(),
- new Class[]{Collection.class},
- new InvocationHandler()
- {
- ArrayList<String> target = new ArrayList<String>();
- public Object invoke(Object proxy, Method method,
- Object[] args) throws Throwable
- {
- Object retVal = method.invoke(target, args);
- return retVal;
- }
-
- }
- );
- proxy.add("nihao");
- proxy.add("it.cast");
- proxy.add("wolaile");
-
- System.out.println(proxy);
- <font color="#ff0000"> Iterator it = proxy.iterator();</font>
- while(it.hasNext())
- {
- System.out.println(it.next());
- }
- }
- }
复制代码 错误提示: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)
|
|