-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectMethod.java
More file actions
38 lines (31 loc) · 1.07 KB
/
Copy pathReflectMethod.java
File metadata and controls
38 lines (31 loc) · 1.07 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
package com.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* @author ManhKM on 4/23/2022
* @project Java-Reflection
*/
public class ReflectMethod {
public static void main(String[] args) {
Class<Girl> girlClass = Girl.class;
Method[] methods = girlClass.getDeclaredMethods();
for (Method method: methods) {
System.out.println();
System.out.println("Method: " + method.getName());
System.out.println("Parameters: " + Arrays.toString(method.getParameters()));
}
try {
Method methodSetName = girlClass.getMethod("setName", String.class);
Girl girl = new Girl();
methodSetName.invoke(girl, "Ngọc Trinh");
System.out.println(girl);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}