- public class ReflectTest {
- public static void main(String[] args) {
-
- Field[] fs = A.class.getFields();
- System.out.println(fs.length); //结果为2,说明public定义的属性全部能被反射出来
-
- Field[] fs1 = AA.class.getFields();
- System.out.println(fs1.length); //结果为零,说明其他修饰符定义的属性都不能用普通方法反射出来
-
- Field[] fs2 = AA.class.getDeclaredFields();//暴力反射
- System.out.println(fs2.length);
- }
-
- }
- class A{
- public String a = "aa";
- public String b = "bb";
- }
- class AA{
- protected String a = "aa";
- String b = "bb";
- private String c = "cc";
- }
复制代码 |
|