Java中getMethods()和getDeclaredMethods()方法有什么不同之处呢?
下文笔者将讲述Java中反射方法getMethods()和getDeclaredMethods()的不同之处,如下所示:
不同之处说明:
getDeclaredMethods:
此方法只返回类中声明的公共方法
getMethods:
此方法返回类中的公共方法及基类中所继承的方法
例:
package com.java265.other;
import java.lang.reflect.Method;
public class test {
/*
* java265.com getMethods同getDeclaredMethods方法的示例分享
*/
public static void main(String[] args) {
Class clazz = test.B.class;
Method t1[] = clazz.getMethods();
Method t2[] = clazz.getDeclaredMethods();
System.out.println("------getMethods------");
for (Method i : t1) {
System.out.println(i.getName());
}
System.out.println("------getDeclaredMethods------");
for (Method i : t2) {
System.out.println(i.getName());
}
}
class A {
private A() {
}
public void b() {
}
private void c() {
}
}
class B extends A {
public B() {
}
private void d() {
}
public void e() {
}
}
}
-------运行以上代码,将输出以下信息-----
------getMethods------
e
b
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
------getDeclaredMethods------
e
d
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


