-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodV2.java
More file actions
25 lines (18 loc) · 957 Bytes
/
Copy pathMethodV2.java
File metadata and controls
25 lines (18 loc) · 957 Bytes
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
package reflection;
import reflection.data.BasicData;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MethodV2 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// 정적 메서드 호출 - 일반적인 메서드 호출
BasicData helloInstance = new BasicData();
helloInstance.call(); // 이 부분은 코드를 변경하지 않는 이상 정적이다.
// 동적 메서드 호출 - 리플랙션 사용
Class<? extends BasicData> helloClass = helloInstance.getClass();
String methodName = "hello";
// 메서드 이름을 변수로 변경할 수 있다.
Method method1 = helloClass.getDeclaredMethod(methodName, String.class);
Object returnValue = method1.invoke(helloInstance, "hi");
System.out.println("returnValue = " + returnValue);
}
}