java - Method.hashCode() 是否考虑方法的参数类型?

标签 java reflection hashcode

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/

相关文章:

java - 测试气味……这是好习惯吗?

java - 如何为字段创建功能接口(interface)实现?

c# - 如何使用反射获取特定接口(interface)类型的所有字段

java - JAVA 的 equals 方法总是返回 true

java - 如何修复 'ClassNotFoundException: brave.propagation.CurrentTraceContext$ScopeDecorator' ?

java - 如何写一个 Vanilla 编年史

Java - TreeSet 和 hashCode()

java - 为什么Hashmap需要覆盖key的hashcode和equals方法?

c# - 将音频转换为文本

c# - 使用反射时如何忽略属性