-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodV3.java
More file actions
34 lines (22 loc) · 1022 Bytes
/
Copy pathMethodV3.java
File metadata and controls
34 lines (22 loc) · 1022 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
26
27
28
29
30
31
32
33
34
package reflection;
import reflection.data.Calculator;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;
public class MethodV3 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Scanner scanner = new Scanner(System.in);
System.out.print("호출 메서드 : ");
String methodName = scanner.nextLine();
System.out.print("숫자1 : ");
int a = scanner.nextInt();
System.out.print("숫자2 : ");
int b = scanner.nextInt();
Calculator calculator = new Calculator();
// 호출할 메서드를 변수 이름으로 동적으로 선택
Class<? extends Calculator> aClass = calculator.getClass();
Method method = aClass.getMethod(methodName, int.class, int.class);
Object returnValue = method.invoke(calculator, a, b);
System.out.println("returnValue = " + returnValue);
}
}