【发布时间】:2010-12-29 14:57:38
【问题描述】:
帮我把这个方法做得更扎实:
/**
* Check if the method is declared in the interface.
* Assumes the method was obtained from a concrete class that
* implements the interface, and return true if the method overrides
* a method from the interface.
*/
public static boolean isDeclaredInInterface(Method method, Class<?> interfaceClass) {
for (Method methodInInterface : interfaceClass.getMethods())
{
if (methodInInterface.getName().equals(method.getName()))
return true;
}
return false;
}
【问题讨论】:
-
你可以有两个同名但参数不同的方法。
-
为什么?这些信息会给你带来什么?
-
为什么人们总是在这里坚持要知道“为什么”? :) 我只想通过 HTTP-GET 从 Hessian servlet 公开这些方法,并且我想过滤不相关的方法 - 通过接口过滤正是我需要的。
-
@ripper234,因为他们可能会针对原始问题提出更简单的解决方案。
-
我们想知道为什么是因为从表面上看,它听起来有点倒退,而您的解释忽略了使情况并非如此所需的细节。例如,如果您知道接口并且知道您的对象实现了该接口,那么为什么不只从接口公开方法而不是从具体类公开方法呢?
标签: java reflection methods