当前位置:网站首页>Get the structure of the class through reflection, little chestnut
Get the structure of the class through reflection, little chestnut
2022-06-23 02:07:00 【Doghead Intern】
public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
Class student = Student.class;
// Get the name of the class
System.out.println(student.getName());// Get package name + Class name
System.out.println(student.getSimpleName());// Get the name of the class
System.out.println("----------------------------------");
// Values obtained public attribute
for (Field field : student.getFields()) {
System.out.println(field);
}
// Get all properties
for (Field declaredField : student.getDeclaredFields()) {
System.out.println(declaredField);
}
Field id = student.getDeclaredField("id");
System.out.println(id);
System.out.println("---------------------------------");
// Get all the methods of this class and its parents
for (Method method : student.getMethods()) {
System.out.println("method "+method);
}
// Get all the methods of this class
for (Method declaredMethod : student.getDeclaredMethods()) {
System.out.println(declaredMethod);
}
System.out.println("---------------------------------");
// Get the specified method
Method getId = student.getMethod("getId", null);
Method getAge = student.getMethod("getAge", null);
Method setName = student.getMethod("setName", String.class);
System.out.println(getId);
System.out.println(getAge);
System.out.println(setName);
System.out.println("---------------------------------");
// Get all the constructors
for (Constructor constructor : student.getConstructors()) {
System.out.println(constructor);
}
for (Constructor declaredConstructor : student.getDeclaredConstructors()) {
System.out.println(declaredConstructor);
}
// Get the specified constructor
Constructor declaredConstructor = student.getDeclaredConstructor(Integer.class, String.class, int.class);
System.out.println(declaredConstructor);
}边栏推荐
- JS prototype and prototype chain Paramecium can understand
- Do you know the memory components of MySQL InnoDB?
- Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)
- [CodeWars] Convert Decimal Degrees to Degrees, Minutes, Seconds
- 1. Mx6u bare metal program (2) - Lighting master (imitating 32 register version)
- Hello
- On function overloading from several examples
- Google benchmark user manual and examples
- Module 8 job
- 2021-11-11
猜你喜欢

Muduo simple usage

Evolution history of mobile communication

2021-11-11

1. introduction to MySQL database connection pool function technology points

Analog Electronic Technology

Function part

5g access network and base station evolution

Performance test -- Jenkins environment construction for 15jmeter performance test

Freshman C language summary post (hold change) Part1 output diamond

5g core network and core network evolution
随机推荐
C. Diluc and Kaeya——Codeforces Round #724 (Div. 2)
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
How to type Redux actions and Redux reducers in TypeScript?
1. Mx6u bare metal program (5) - external interrupt
2022-1-14
Evolution history of mobile communication
Interviewer: with the for loop, why do you need foreach??
Bc117 xiaolele walks up the steps
Nfv and SDN
Nebula operator cloud practice
Ch340 and PL2303 installation (with link)
Deep learning environment configuration (III) pytorch GPU under Anaconda
Detailed explanation of GCC usage
2022-1-12
Foundation Consolidation - Flex width is content width
Detailed explanation of makefile usage
Express framework installation and start service
Digital integrated circuit design process
How are pub and sub connected in ros1?
5. explain function overloading