forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvokeMethod.java
More file actions
84 lines (72 loc) · 2.62 KB
/
InvokeMethod.java
File metadata and controls
84 lines (72 loc) · 2.62 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cn.aofeng.demo.java.lang.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static cn.aofeng.demo.util.LogUtil.log;
/**
* 通过反射调用方法。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class InvokeMethod {
/**
* 调用父类的方法。
*
* @param claz
* 类
* @param man
* 类对应的实例
*/
private static void invokeParentMethod(Class<Man> claz, Man man)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
// 调用父类的public方法
Method method = claz.getMethod("setName", String.class);
method.invoke(man, "NieYong");
log(man.toString());
method = claz.getMethod("getName");
Object result = method.invoke(man);
log("name:%s", result);
// 调用父类的private方法
method = claz.getSuperclass().getDeclaredMethod("reset");
method.setAccessible(true);
result = method.invoke(man);
log(man.toString());
}
/**
* 调用自身的方法。
*
* @param claz
* 类
* @param man
* 类对应的实例
*/
private static void invokeSelfMethod(Class<Man> claz, Man man)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
man.setName("XiaoMing");
// 调用自身的private方法
Method method = claz.getDeclaredMethod("setPower", int.class);
method.setAccessible(true);
method.invoke(man, 99);
log("power:%d", man.getPower());
// 调用自身的public方法
log("%s is marry:%s", man.getName(), (man.isMarry() ? "Yes" : "No"));
method = claz.getDeclaredMethod("setMarry", boolean.class);
method.invoke(man, true);
log("%s is marry:%s", man.getName(), (man.isMarry() ? "Yes" : "No"));
// 调用静态方法,可将实例设置为null,因为静态方法属于类
Man a = new Man("张三");
Man b = new Man("李四");
method = claz.getMethod("fight", Man.class, Man.class);
method.invoke(null, a, b); //
}
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
InstantiationException {
Class<Man> claz = Man.class;
Man man = claz.newInstance();
invokeParentMethod(claz, man);
invokeSelfMethod(claz, man);
}
}