1
我已经阅读了很多关于反射的文章,所有的例子都是简单的字符串访问对象,double和int等。 但是,我想访问像Widget,Text甚至自定义对象的对象。 我尝试了与字符串相同的方式,但失败。
例如
我可以访问小部件,文本对象的私人领域吗?
class testPrivate{
public boolean test()
{
return true;
}
}
class button {
public button(){
anc=new testPrivate();
}
private testPrivate anc;
}
public class Testing {
public static void main(String arg[]) throws Throwable{
button bt=new button();
Field field = bt.getClass().getDeclaredField("anc");
field.setAccessible(true);
System.out.println(field.test());
}
}
这里,field.test()在声明中的System.out.println(field.test());失败。