forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest05.java
More file actions
66 lines (48 loc) · 1.97 KB
/
Test05.java
File metadata and controls
66 lines (48 loc) · 1.97 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test05 {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class.forName("reflection.User");
System.out.println(c1.getName());//获得包名+类名
System.out.println(c1.getSimpleName()); //获得类名
//获得类的属性
System.out.println("===============================");
Field[] fields = c1.getDeclaredFields();
for (Field item : fields) {
System.out.println(item);
}
System.out.println("=============获取指定的属性值================");
Field name = c1.getDeclaredField("name");
System.out.println(name);
System.out.println("=============获取类的方法================");
Method[] methods = c1.getMethods();
for (Method item : methods) {
System.out.println(item);
}
System.out.println("=============获取类的方法,使用getDeclaredMethods================");
Method[] methods1 = c1.getDeclaredMethods();
for (Method item : methods1) {
System.out.println(item);
}
System.out.println("=============获取类的指定方法================");
Method getName = c1.getMethod("getName", null);
System.out.println(getName);
Method setName = c1.getMethod("setName", String.class);
System.out.println(setName);
System.out.println("=============获取构造器================");
Constructor[] constructors = c1.getConstructors();
for (Constructor item : constructors) {
System.out.println(item);
}
Constructor[] constructors1 = c1.getDeclaredConstructors();
for (Constructor item : constructors1) {
System.out.println(item);
}
Constructor declaredConstructor = c1
.getDeclaredConstructor(String.class, String.class, int.class);
System.out.println("指定构造器" + declaredConstructor);
}
}