java反射中getDeclaredMethods和getMethods的区别
区别在于是否可以获取非public方法
1.getDeclaredMethods是可以获取非public方法的:
getDeclaredMethods():
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type, an array class, or void. The class initialization method <clinit> is not included in the returned array. If the class declares multiple public member methods with the same parameter types, they are all included in the returned array.
注意“This includes public, protected, default (package) access, and private methods”,说明他是可以获取public, protected, default (package) access, and private methods这四种方法的。但是从父类继承来的不算:“but excludes inherited methods.”
2 .getMethods是只可以获取public方法的
Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces. Array classes return all the (public) member methods inherited from the Object class. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if thisClass object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.
注意“Array classes return all the (public) member methods inherited from the Object class. ”,说明他只能获取用public修饰的方法。
以上所有的资料全部摘自Javadoc(链接百度不让贴,这叫广告)l
public Method[] getMethods()
throws SecurityException返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。数组类返回从 Object 类继承的所有(公共)member 方法。返回数组中的元素没有排序,也没有任何特定的顺序。如果此 Class 对象表示没有公共成员方法的类或接口,或者表示一个基本类型或 void,则此方法返回长度为 0 的数组。
getDeclaredMethods
public Method[] getDeclaredMethods()
throws SecurityException返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。返回数组中的元素没有排序,也没有任何特定的顺序。如果该类或接口不声明任何方法,或者此 Class 对象表示一个基本类型、一个数组类或 void,则此方法返回一个长度为 0 的数组。类初始化方法 <clinit> 不包含在返回数组中。如果该类声明带有相同参数类型的多个公共成员方法,则它们都包含在返回的数组中。
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.
返回一个包含全部由该类声明的方法或者接口的数组,包含 public protected,default 以及private 修饰的,但是不包含 继承关系的方法。
getMethods
Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
返回一个包含全部由该类声明的public修饰的方法及接口的数组,包含当前类及继承关系类的方法及接口。
1,getDeclaredMethods能拿到所有(不包括继承的方法);
2,getMethods只能拿到public方法(包括继承的类或接口的方法)
其他都一样。