gpt4 book ai didi

java - getMethod 避免父类

转载 作者:行者123 更新时间:2023-11-29 03:33:37 26 4
gpt4 key购买 nike

在使用 getMethod() 时,我遇到了一个问题。我正在调用 getMethod() 的类有许多父方法。但是,我不希望 getMethod 注意到父类的方法,只希望看到我正在查看的特定类。例如……

class superClass {
boolean equals(Object obj) {
....
}
}

...

import superClass

class subClass {
...
}

现在如果我像这样使用 getMethod...

try{
Class[] args = new Class[1];
args[0] = Object.class;
Method equalsMethod = subClass.getMethod("equals", args);
}

catch(NoSuchMethodException ex){
...
}

我不希望它从父类(super class)中引入 equals 方法,而它目前正在这样做。我只想知道我调用 getMethod 的类(在本例中为子类)是否包含方法 equals()。

有什么办法吗?任何帮助将不胜感激。

最佳答案

试试 getDeclaredMethod(String, args)。它只会返回您的类显式声明的方法,因此不涉及父类(super class)。

关于java - getMethod 避免父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16658031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com