0%

获取运行时类结构

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
package com.sympa.lesson01;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionTest {

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

Class<Person> clazz = Person.class;

//newInstance:创建对应运行时类对象
Person person = clazz.getDeclaredConstructor().newInstance();
System.out.println(person);

//获取运行时类的属性结构
Field[] fields = clazz.getDeclaredFields();
for(Field f : fields){
System.out.println(f);
}

//获取运行时类的方法结构
Method[] methods = clazz.getDeclaredMethods();
for(Method m : methods){
System.out.println(m);
}

Method show = clazz.getMethod("show");

//…………………………
}
}
------ THEEND ------

欢迎关注我的其它发布渠道