Java 反射:getMethods() 和 getDeclaredMethods() 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43585019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Reflection: Difference between getMethods() and getDeclaredMethods()
提问by Rakib
Can someone please elaborate on this, and explain the difference between the two methods, and when/why you would want to use one over the others
有人可以详细说明这一点,并解释这两种方法之间的区别,以及何时/为什么要使用一种方法而不是其他方法
回答by Thilo
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
仅返回公共方法,但也包括从基类(此处为 from java.lang.Object
)继承的方法。
Read more about it in the Javadocs for getDeclaredMethod
and getMethods
.
在 Javadocs for getDeclaredMethod
and 中阅读更多关于它的信息getMethods
。