我的代码中有一个小问题
我有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?我该怎么办呢?
谢谢