首页  

反射获取字段注意点     所属分类 java 浏览量 1374
getFields 获取 public 字段 (含超类)


getDeclaredFields 只能获取当前类定义的字段 ( private protected public)

反射获取方法类似



GrandFather.java public class GrandFather{ private static String privateStaticFieldGrandFather = "privateStaticFieldGrandFather-value"; protected static String protectedStaticFieldGrandFather = "protectedStaticFieldGrandFather-value"; public static String publicStaticFieldGrandFather = "publicStaticFieldGrandFather-value"; private String privateFieldGrandFather = "privateFieldGrandFather-value"; protected String protectedFieldGrandFather = "protectedFieldGrandFather-value"; public String publicFieldGrandFather = "publicFieldGrandFather-value"; }
Father.java public class Father extends GrandFather{ private static String privateStaticFieldFather = "privateStaticFieldFather-value"; protected static String protectedStaticFieldFather = "protectedStaticFieldFather-value"; public static String publicStaticFieldFather = "publicStaticFieldFather-value"; private String privateFieldFather = "privateFieldFather-value"; protected String protectedFieldFather = "protectedFieldFather-value"; public String publicFieldFather = "publicFieldFather-value"; }
Son.java public class Son extends Father{ private static String privateStaticField = "privateStaticField-value"; protected static String protectedStaticField = "protectedStaticField-value"; public static String publicStaticField = "publicStaticField-value"; private String privateField = "privateField-value"; protected String protectedField = "protectedField-value"; public String publicField = "publicField-value"; }
System.out.println(Son.class.getSuperclass()); System.out.println(Son.class.getSuperclass().getSuperclass()); System.out.println(); Field[] fields = Son.class.getFields(); for(Field field:fields){ System.out.println(field.getName()); } System.out.println(); fields = Son.class.getDeclaredFields(); for (Field field : fields) { System.out.println(field.getName()); } System.out.println(); Son child = new Son(); Field field = Son.class.getField("publicStaticField"); System.out.println(field.get(null)); field = Son.class.getField("publicStaticFieldFather"); System.out.println(field.get(null)); field = Son.class.getField("publicStaticFieldGrandFather"); System.out.println(field.get(null)); field = Son.class.getField("publicField"); System.out.println(field.get(child)); field = Son.class.getField("publicFieldFather"); System.out.println(field.get(child)); field = Son.class.getField("publicFieldGrandFather"); System.out.println(field.get(child)); System.out.println(); field = Son.class.getDeclaredField("privateStaticField"); field.setAccessible(true); System.out.println(field.get(null)); field = Son.class.getDeclaredField("privateField"); field.setAccessible(true); System.out.println(field.get(child));
class field.Father class field.GrandFather publicStaticField publicField publicStaticFieldFather publicFieldFather publicStaticFieldGrandFather publicFieldGrandFather privateStaticField protectedStaticField publicStaticField privateField protectedField publicField publicStaticField-value publicStaticFieldFather-value publicStaticFieldGrandFather-value publicField-value publicFieldFather-value publicFieldGrandFather-value privateStaticField-value privateField-value

上一篇     下一篇
eclipse异常断点设置

eclipse设置条件断点和异常断点

spring5属性及内嵌属性解析

这样的人你敢要吗

我们怎么了

做局与破局