
您可以使用反射
public static void main(String[] args) throws Exception { Method method = Bar.class.getMethod("hashCode" ); // pass parameter types as needed System.out.println(method); System.out.println(overridesMethod(method, Bar.class));}public static boolean overridesMethod(Method method, Class<?> clazz) { return clazz == method.getDeclaringClass();}class Bar { }false如果将
hashCode()注释掉,
true如果没有注释,将打印。
Method#getDeclaringClass()将返回实现该
Class对象的类的对象。
请注意
,仅
Class#getMethod(..)适用于
public方法。但是,在这种情况下,
equals()和
hashCode()必须
public。取决于其他算法,该算法将需要更改。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)