Skip to content

Commit a63ea9c

Browse files
committed
add
1 parent f1b586e commit a63ea9c

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed
18 Bytes
Binary file not shown.
15 Bytes
Binary file not shown.
24 Bytes
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.duwei.reflect.method;
2+
3+
public interface MethodInterface {
4+
5+
public void MethodInInterface();
6+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

0 commit comments

Comments
 (0)