首页 > 代码库 > 【反射之Field】获取字段

【反射之Field】获取字段

 ■getFields()、getDeclaredFields()、getField() 和 getDeclaredField()的用法

 

 1 package reflect.field;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.Field;
 5 import java.lang.reflect.InvocationTargetException;
 6 import java.lang.reflect.Method;
 7 
 8 public class FieldMethod {
 9 
10     public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
11         Class<?> personClass = Class.forName("reflect.Person");
12         
13         Constructor<?> constructor = personClass.getDeclaredConstructor(String.class, int.class, double.class, String.class);
14         Object p = constructor.newInstance("周娟娟", 23, 80, "14计科1班");
15         System.out.println(p.toString());
16         
17         
18         // 该方法只能获取到公有的字段(属性)
19         Field[] fields = personClass.getFields();
20         for (Field field : fields) {
21 //            System.out.println(field);
22         }
23         
24         // 该方法能获取到公有的、私有的、受保护的字段
25         Field[] declaredFields = personClass.getDeclaredFields();
26         for (Field field : declaredFields) {
27 //            System.out.println(field);
28         }
29         
30         // 该方法只能获取到公有的字段,若传入的是私有或受保护的则会报错NoSuchFieldException
31         Field nameFieldPublic = personClass.getField("className");
32         
33         nameFieldPublic.set(p, "卓越班");
34         Method getClassName = personClass.getMethod("getClassName");
35         Object className = getClassName.invoke(p);
36         System.out.println(className);
37         
38         // 该方法可以获取到公有的、私有的、受保护的属性
39         Field nameFieldPrivate = personClass.getDeclaredField("name");
40         
41         nameFieldPrivate.setAccessible(true);
42 //        System.out.println(nameFieldPrivate);
43         nameFieldPrivate.set(p, "Jay");
44         System.out.println(p);
45         
46         // 获取到私有属性age
47         Field ageFieldPrivate = personClass.getDeclaredField("age");
48         // 忽略修饰符访问检查
49         ageFieldPrivate.setAccessible(true);
50         // 设置age的值,必须指明修改的是哪个对象的属性
51         ageFieldPrivate.set(p, 24);
52         System.out.println(p);
53         
54     }
55 
56 }

 

 1 package reflect;
 2 
 3 public class Person {
 4     
 5     private String name;
 6     private int age;
 7     protected double weight;
 8     public String className;
 9     
10     public Person() {
11     }
12     
13     private Person(int age) {
14         
15     }
16     
17     public Person(String name, int age, double weight, String className) {
18         super();
19         this.name = name;
20         this.age = age;
21         this.weight = weight;
22         this.className = className;
23     }
24 
25     public double getWeight() {
26         return weight;
27     }
28 
29     public void setWeight(double weight) {
30         this.weight = weight;
31     }
32 
33     public String getClassName() {
34         return className;
35     }
36 
37     public void setClassName(String className) {
38         this.className = className;
39     }
40 
41     public String getName() {
42         return name;
43     }
44 
45     public void setName(String name) {
46         this.name = name;
47     }
48 
49     private int getAge() {
50         return age;
51     }
52 
53     public void setAge(int age) {
54         this.age = age;
55     }
56 
57     @Override
58     public String toString() {
59         return "Person [name=" + name + ", age=" + age + ", weight=" + weight
60                 + ", className=" + className + "]";
61     }
62 
63     
64 
65 }

 

【反射之Field】获取字段