public static synchronized void main(String[] a) throws Exception {
T t = new T();
/*Method m = t.getClass().getMethod("GreedSnake");
System.out.println(m.invoke(t));*/
List<T> list = new ArrayList<T>();
list.add(t);
Iterator<T> it = list.iterator();
Method m = it.getClass().getMethod("hasNext");
System.out.print(m.invoke(it));
}</P>
<P> public boolean hasNext() {
return true;
}</P>
<P>}</P>
复制代码
错误信息:
Exception in thread "main" java.lang.IllegalAccessException: Class T can not acc
ess a member of class java.util.AbstractList$Itr with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at T.main(T.java:20)
我想调用hasNext方法时,提示错误,但是在类中我已有hasNext()了,求大大指点.
作者: 衣带赞 时间: 2013-11-28 15:56
你调用的是list.iterator,和你这个类中写的hasNext()方法没关系,把你这个类中写的hasNext()方法注释掉,没有区别。
再看这个错误信息:Exception in thread "main" java.lang.IllegalAccessException: Class T can not acc
ess a member of class java.util.AbstractList$Itr with modifiers "public"
非法接收异常:T这个类不可以接收java.util.AbstractList$Itr这个类中用 "public"修饰的成员。