在使用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){
...
}我不希望它从superClass中拉入equals方法,这是它目前正在做的。我只想知道我调用getMethod的类(在本例中是subClass)是否包含equals()方法。
有什么办法做到这一点吗?任何帮助都将不胜感激。
发布于 2013-05-21 04:46:30
尝试使用getDeclaredMethod(String,args)。它只会返回你的类显式声明的方法,所以不涉及supers。
发布于 2013-05-21 04:48:36
请改用subClass.getDeclaredMethod()。这样,您将获得在类或MethodNotFoundException中明确声明的方法
https://stackoverflow.com/questions/16658031
复制相似问题