小编Daz*_*zel的帖子

GetDeclaredMethods(java)中的问题

我的代码中有一个小问题

我有2节课

public class A {

     public A foo(int a) {return new A();}
}

public class B extends A{

     public B foo(int x){ return new B();}
}
Run Code Online (Sandbox Code Playgroud)

现在在我的代码中我想只打印在B类中声明的方法

通过这种方式

B b = new B();

Method[] m = b.getClass().getDeclaredMethods();

for (int i = 0; i < m.length; i++) {

System.out.print(m[i].getName());   
}
Run Code Online (Sandbox Code Playgroud)

为什么输出

foo

foo
Run Code Online (Sandbox Code Playgroud)

为什么GetDeclaredMethods在A类中也找到了foo?我该怎么办呢?

谢谢

java reflection inheritance

8
推荐指数
2
解决办法
6080
查看次数

标签 统计

inheritance ×1

java ×1

reflection ×1