public class TestBean
{
private String name = "abc";
}
public class PrivateTest
{
public static void main( String[] args ) throws IllegalArgumentException, IllegalAccessException
{
TestBean tb = new TestBean();
Field[] f = TestBean.class.getDeclaredFields();
for(int i=0;i<f.length;i++){
f[i].setAccessible( true );
System.out.println(f[i].get( tb ));
}
}
}
私有变量的值是可以访问到的 输出为abc
那设置成私有是不是没用啊~~~那封装还有什么用? |
|