File tree Expand file tree Collapse file tree 6 files changed +86
-0
lines changed
bin/com/duwei/thinkingjava/reflect
src/com/duwei/reflect/method Expand file tree Collapse file tree 6 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .duwei .reflect .method ;
2+
3+ public class MethodFather implements MethodInterface {
4+ @ Override
5+ public void MethodInInterface () {
6+
7+ }
8+
9+ public void MethodInFatherPublic (){
10+
11+ }
12+
13+ private void MethodInFatherPrivate (){
14+
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .duwei .reflect .method ;
2+
3+ public interface MethodInterface {
4+
5+ public void MethodInInterface ();
6+ }
Original file line number Diff line number Diff line change 1+ package com .duwei .reflect .method ;
2+
3+ import java .lang .reflect .Method ;
4+
5+ /**
6+ * 演示了clazz.getMethods();和clazz.getDeclaredMethods();的区别
7+ * getMethods 只能拿到public方法(包括继承的类或接口的方法)
8+ * getDeclaredMethods 拿到类或接口声明的所有方法,包括公共、保护、
9+ * 默认(包)访问和私有方法,但不包括继承的方法。
10+ */
11+
12+ public class MethodVsDeclaredMethod extends MethodFather implements Runnable {
13+
14+ public static void main (String [] args ) {
15+
16+ Class clazz = MethodVsDeclaredMethod .class ;
17+
18+ Method [] methods = clazz .getMethods ();
19+
20+ Method [] declaredMethods = clazz .getDeclaredMethods ();
21+
22+ for (Method method : methods ) {
23+ System .out .println (method .getName ());
24+ /**
25+ * main
26+ *MethodInInterface
27+ *MethodInFatherPublic
28+ *wait
29+ *wait
30+ *wait
31+ *equals
32+ *toString
33+ *hashCode
34+ *getClass
35+ *notify
36+ *notifyAll
37+ */
38+ }
39+
40+ System .out .println ("---------------" );
41+
42+ for (Method method : declaredMethods ) {
43+ System .out .println (method .getName ());
44+ /**
45+ * main
46+ */
47+ }
48+
49+ }
50+
51+ private void methodDemoPrivate (){
52+
53+ }
54+
55+ public void methodDemoPublic (){
56+
57+ }
58+
59+ @ Override
60+ public void run () {
61+
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments