[英]Java Reflection: Difference between getMethods() and getDeclaredMethods()
有人可以详细说明一下,并解释这两种方法之间的区别,以及何时/为什么要使用其中一种方法而不是其他方法
getDeclaredMethods
includes all methods declared by the class itself , whereas getMethods
returns only public methods, but also those inherited from a base class (here from java.lang.Object
). getDeclaredMethods
包括由类本身声明的所有方法,而getMethods
只返回公共方法,但也包括从基类(这里是java.lang.Object
)继承的方法。
Read more about it in the Javadocs for getDeclaredMethod
and getMethods
.在
getDeclaredMethod
和getMethods
的 Javadocs 中阅读有关它的更多信息。
Method![]() |
Public![]() |
Non-public![]() |
Inherited![]() |
---|---|---|---|
getMethods() |
✔️ ![]() |
❌ ![]() |
✔️ ![]() |
getDeclaredMethods() |
✔️ ![]() |
✔️ ![]() |
❌ ![]() |
Methods![]() |
getMethods()![]() |
getDeclaredMethods ![]() |
---|---|---|
public![]() |
✔️ ![]() |
✔️ ![]() |
protected![]() |
❌ ![]() |
✔️ ![]() |
private![]() |
❌ ![]() |
✔️ ![]() |
static public![]() |
✔️ ![]() |
✔️ ![]() |
static protected![]() |
❌ ![]() |
✔️ ![]() |
static private![]() |
❌ ![]() |
✔️ ![]() |
default public![]() |
✔️ ![]() |
✔️ ![]() |
default protected![]() |
❌ ![]() |
✔️ ![]() |
default private![]() |
❌ ![]() |
✔️ ![]() |
inherited public![]() |
✔️ ![]() |
❌ ![]() |
inherited protected![]() |
❌ ![]() |
❌ ![]() |
inherited private![]() |
❌ ![]() |
❌ ![]() |
inherited static private![]() |
✔️ ![]() |
❌ ![]() |
inherited static protected![]() |
❌ ![]() |
❌ ![]() |
inherited static private![]() |
❌ ![]() |
❌ ![]() |
default inherited public![]() |
✔️ ![]() |
❌ ![]() |
default inherited protected![]() |
❌ ![]() |
❌ ![]() |
default inherited private![]() |
❌ ![]() |
❌ ![]() |
If your goal, like mine, was to get public methods of a class:如果你的目标和我一样,是获取一个类的公共方法:
Method![]() |
Public![]() |
Non-public![]() |
Inherited![]() |
---|---|---|---|
getMethods() |
✔️ ![]() |
❌ ![]() |
✔️ ![]() |
getDeclaredMethods() |
✔️ ![]() |
✔️ ![]() |
❌ ![]() |
getPublicMethods()![]() |
✔️ ![]() |
❌ ![]() |
❌ ![]() |
and nothing else:仅此而已:
Methods![]() |
getPublicMethods()![]() |
---|---|
public![]() |
✔️ ![]() |
protected![]() |
❌ ![]() |
private![]() |
❌ ![]() |
static public![]() |
❌ ![]() |
static protected![]() |
❌ ![]() |
static private![]() |
❌ ![]() |
default public![]() |
❌ ![]() |
default protected![]() |
❌ ![]() |
default private![]() |
❌ ![]() |
inherited public![]() |
❌ ![]() |
inherited protected![]() |
❌ ![]() |
inherited private![]() |
❌ ![]() |
inherited static private![]() |
❌ ![]() |
inherited static protected![]() |
❌ ![]() |
inherited static private![]() |
❌ ![]() |
default inherited public![]() |
❌ ![]() |
default inherited protected![]() |
❌ ![]() |
default inherited private![]() |
❌ ![]() |
You have to do it yourself:你必须自己做:
Iterable<Method> getPublicMethods(Object o) {
List<Method> publicMethods = new ArrayList<>();
// getDeclaredMethods only includes methods in the class (good)
// but also includes protected and private methods (bad)
for (Method method : o.getClass().getDeclaredMethods()) {
if (!Modifier.isPublic(method.getModifiers())) continue; //only **public** methods
if (!Modifier.isStatic(method.getModifiers())) continue; //only public **methods**
publicMethods.add(method);
}
return publicMethods;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.