Javadocs 说:
Returns a hashcode for this Method. The hashcode is computed as the exclusive-or of the hashcodes for the underlying method's declaring class name and the method's name.
此描述中明显缺少方法参数类型的类型 - 这是否意味着同一类中具有相同名称但参数不同的两个方法将具有相同的 hashCode()
?
最佳答案
您是对的 - 具有相同名称和相同声明类的方法具有相同的哈希码,如文档所述。我同意,这有点违反直觉。
Sun的JDK中的代码:
public int hashCode() {
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
}
但是 hashCode()
不是相等的标志。 equals(..)
方法考虑参数。
关于java - Method.hashCode() 是否考虑方法的参数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5519261/