Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
41 lines (32 loc) · 1.26 KB

File metadata and controls

41 lines (32 loc) · 1.26 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.reflectpro;
import org.junit.Test;
import java.lang.reflect.Field;
public class GetFiled {
/*
获取指定名字的属性 getDeclaredField() getField()
getDeclaredField()获取的是 Class 中被 private 修饰的属性。
getField() 方法获取的是非私有属性,并且 getField()在当前 Class 获取不到时会向祖先类获取。
*/
/*
getDeclaredFields()获取所有的属性,但不包括从父类继承下来的属性
getFields()获取自身的所有的 public 属性,包括从父类继承下来的。
*/
@Test
public void getFiledDemo1() throws Exception {
//本类没有,从父类获取
Field a = Son.class.getField("a");
System.out.println("a=" + a);
Field d = Son.class.getDeclaredField("d");
System.out.println("d=" + d);
//获取public属性,包括继承来的
Field[] fields1 = Son.class.getFields();
for (Field f: fields1) {
System.out.println("fields"+f.getName());
}
//获取所有属性,但是不包括继承来的
Field[] fields2 = Son.class.getDeclaredFields();
for (Field f: fields2) {
System.out.print("DeclaredFields:"+f.getName()+" ");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.