Object.hashCode() 的默认行为本质上是返回对象的“地址”,因此当且仅当 a == b 时 a.hashCode() == b.hashCode()。如果超类已经定义了 hashCode(),如何在用户定义的类中获得这种行为?例如:
class A {
public int hashCode() {
return 0;
}
}
class B extends A {
public int hashCode() {
// Now I want to return a unique hashcode for each object.
// In pythonic terms, it'd look something like:
return Object.hashCode(this);
}
}
想法?