请写出如下代码的运行结果(22)
import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class T { public static synchronized void main(String[] a) throws Exception { T t = new T(); Method m = t.getClass().getMethod("hasNext"); System.out.print(m.invoke(t)); List<T> list = new ArrayList<T>(); list.add(t); Iterator it = list.iterator(); m = it.getClass().getMethod("hasNext"); System.out.print(m.invoke(it)); } public boolean hasNext() { return true; }}import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class T { public static synchronized void main(String[] a) throws Exception { T t = new T(); Method m = t.getClass().getMethod("hasNext"); System.out.print(m.invoke(t)); List<T> list = new ArrayList<T>(); list.add(t); Iterator it = list.iterator(); m = it.getClass().getMethod("hasNext"); System.out.print(m.invoke(it)); } public boolean hasNext() { return true; }}
[解决办法]
import java.lang.reflect.Method;public class T { public static void main(String[] args)throws Exception{ T t = new T(); Sam sam = t.getIt(); Method m = sam.getClass().getMethod("f"); System.out.println(sam.getClass()); m.invoke(sam); } private class It implements Sam{ public void f(){ System.out.println("F method"); } } public Sam getIt(){ return new It(); }}public interface Sam{ public void f();}
[解决办法]
原因我找到了,是package的问题,请看如下代码:
下面是3个模仿的类,用于测试,请注意其package为 a
package a;public interface MyCollection { public MyIterator myIterator();}
[解决办法]
mark
[解决办法]
会不会 先顶在说!
[解决办法]
Itr是一个inner Class
[解决办法]
m.setAccessible(true);//注意这句
System.out.print(m.invoke(it));
}
public boolean hasNext() {
return true;
}
}
2.用内部类实现的接口构造Method对象
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class T {
public static synchronized void main(String[] a) throws Exception {
T t = new T();
Method m = t.getClass().getMethod("hasNext");
System.out.print(m.invoke(t));
List<T> list = new ArrayList<T>();
list.add(t);
Iterator it = list.iterator();
m = Iterator.class.getMethod("hasNext");//注意这句
System.out.print(m.invoke(it));
}
public boolean hasNext() {
return true;
}
}
[解决办法]
第二种方法也可以充分说明,虽然内部类是私用的,但它的公共方法在其它包内调用是没有任何问题的.只是调用时不能用原始类型(不可见),必须向上转型后才可调用.
[解决办法]
附整理后的新的测试代码,配合我13楼的代码
package b;import java.lang.reflect.Method;import a.MyArrayList;import a.MyCollection;import a.MyIterator;/*** * @author 赵学庆 <A href="http://www.java2000.net/" target=_blank>www.java2000.net</A>* @thanks Ant_Yan xunyiren**/public class TestInvoke { public static void main(String[] args) throws Exception { test1(); test2(); test3(); } public static void test1() { MyCollection t = new MyArrayList(); MyIterator sam = t.myIterator(); Method m; try { m = sam.getClass().getMethod("myHasNext"); m.invoke(sam); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void test2() { MyCollection t = new MyArrayList(); MyIterator sam = t.myIterator(); Method m; try { m = sam.getClass().getMethod("myHasNext"); m.setAccessible(true); // 此处设置了取消安全检测 m.invoke(sam); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void test3() { MyCollection t = new MyArrayList(); MyIterator sam = t.myIterator(); Method m; try { m = MyIterator.class.getMethod("myHasNext"); // 此处使用了接口自身 m.invoke(sam); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}