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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:34:05  来源:igfitidea点击:

Java Reflection: Difference between getMethods() and getDeclaredMethods()

java

提问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

getDeclaredMethodsincludes all methods declared by the class itself, whereas getMethodsreturns 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 getDeclaredMethodand getMethods.

在 Javadocs for getDeclaredMethodand 中阅读更多关于它的信息getMethods