因为不是public的 所有需要用getDeclaredFields();来获取,然后要进行暴力反射 具体代码如下
- 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 ));
- }
- }
- }
复制代码 |