0%

Java 拾遗之 反射

记录一些 Java 反射基础知识

准备测试 Bean

1
2
3
4
5
6
7
8
9
package reflectiontest.bean;

public class TestUser {
private String name;
private int age;
public String gender;

// Getter and Setter
}

getFields VS getDeclaredFields

getFields 只会返回 public 类型的 fields, getDeclaredFields 会返回所有类型的 fieds

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Test
public void get_class_field() {
Field[] fields = TestUser.class.getFields();
System.out.println("Output of getFields...");
for (Field f : fields) {
System.out.println(f);
}
System.out.println("\n");

Field[] declareFields = TestUser.class.getDeclaredFields();
System.out.println("Output of getDeclaredFields...");
for (Field f : declareFields) {
System.out.println(f);
}
}

// Output of getFields...
// public java.lang.String reflectiontest.bean.TestUser.gender


// Output of getDeclaredFields...
// private java.lang.String reflectiontest.bean.TestUser.name
// private int reflectiontest.bean.TestUser.age
// public java.lang.String reflectiontest.bean.TestUser.gender