forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvokeMethodDemo.java
More file actions
47 lines (41 loc) · 1.68 KB
/
InvokeMethodDemo.java
File metadata and controls
47 lines (41 loc) · 1.68 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
39
40
41
42
43
44
45
46
47
package onlyfun.caterpillar;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InvokeMethodDemo {
public static void main(String[] args) {
try {
Class c = Class.forName(args[0]);
// 使用無參數建構方法建立物件
Object targetObj = c.newInstance();
// 設定參數型態
Class[] param1 = {String.class};
// 根據參數型態取回方法物件
Method setNameMethod = c.getMethod("setName", param1);
// 設定引數值
Object[] argObjs1 = {"caterpillar"};
// 給定引數呼叫指定物件之方法
setNameMethod.invoke(targetObj, argObjs1);
Class[] param2 = {Integer.TYPE};
Method setScoreMethod =
c.getMethod("setScore", param2);
Object[] argObjs2 = {new Integer(90)};
setScoreMethod.invoke(targetObj, argObjs2);
// 顯示物件描述
System.out.println(targetObj);
} catch (ClassNotFoundException e) {
System.out.println("找不到類別");
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
System.out.println("沒有這個方法");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}