前言
最近在使用getDeclaredMethods
方法获取类中的方法时碰到一个奇怪的问题,先来看看getDeclaredMethods
方法的注释:
Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.
谷歌翻译:
返回一个包含 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口的所有声明方法,包括 public,protected,default(包)访问和私有方法,但不包括继承的方法。
注意加粗的字体,可以看到 JDK 注释里明确的说明了getDeclaredMethods
方法不会返回继承的方法,我要的功能就是取当前类上的方法(不包含父类的),但是事情并没有这么简单,下面一起来看看是为什么。