覆盖equals()&子类中的hashCode()...考虑超级字段 [英] Overriding equals() & hashCode() in sub classes ... considering super fields
问题描述
是否有关于如何覆盖等于()
&的特定规则考虑超级字段,子类中的 hashCode()
知道有很多参数:超级字段是私有/公共的,有/无getter ...
Is there a specific rule on how Overriding equals()
& hashCode()
in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ...
例如,Netbeans生成了equals()& hashCode()不会考虑超级字段......和
For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and
new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals(
new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot"))
将返回true :(
will return true :(
public class Hominidae {
public String gender;
public String weight;
public String height;
public Hominidae(String gender, String weight, String height) {
this.gender = gender;
this.weight = weight;
this.height = height;
}
...
}
public class HomoSapiens extends Hominidae {
public String name;
public String faceBookNickname;
public HomoSapiens(String gender, String weight, String height,
String name, String facebookId) {
super(gender, weight, height);
this.name = name;
this.faceBookNickname = facebookId;
}
...
}
如果你想看看Netbeans生成的equals()& hashCode():
If you want to see the Netbeans generated equals() & hashCode() :
public class Hominidae {
...
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Hominidae other = (Hominidae) obj;
if ((this.gender == null) ? (other.gender != null) : !this.gender.equals(other.gender)) {
return false;
}
if ((this.weight == null) ? (other.weight != null) : !this.weight.equals(other.weight)) {
return false;
}
if ((this.height == null) ? (other.height != null) : !this.height.equals(other.height)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 5;
hash = 37 * hash + (this.gender != null ? this.gender.hashCode() : 0);
hash = 37 * hash + (this.weight != null ? this.weight.hashCode() : 0);
hash = 37 * hash + (this.height != null ? this.height.hashCode() : 0);
return hash;
}
}
public class HomoSapiens extends Hominidae {
...
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final HomoSapiens other = (HomoSapiens) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if ((this.faceBookNickname == null) ? (other.faceBookNickname != null) : !this.faceBookNickname.equals(other.faceBookNickname)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 89 * hash + (this.faceBookNickname != null ? this.faceBookNickname.hashCode() : 0);
return hash;
}
}
推荐答案
< b>孩子不应该检查父母的私人成员
但显然是,所有重要的字段都应该考虑在内平等和散列。
But obviously, all significant fields should be taken into account for equality and hashing.
幸运的是,你可以很容易地满足这两个规则。
Fortunately, you you can easily satisfy both rules.
假设你没有卡住使用NetBeans生成的equals和hashcode,您可以修改Hominidae的equals方法以使用instanceof比较而不是类相等,然后直接使用它。这样的事情:
Assuming you're not stuck using the NetBeans-generated equals and hashcode, you can modify Hominidae's equals method to use instanceof comparison rather than class equality, and then use it straightforwardly. Something like this:
@Override
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (getClass() != obj.getClass()) { return false; }
if (! super.equals(obj)) return false;
else {
// compare subclass fields
}
当然,哈希码很简单:
@Override
public int hashCode() {
int hash = super.hashCode();
hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 89 * hash + (this.faceBookNickname != null ? this.faceBookNickname.hashCode() : 0);
return hash;
}
但是,严重的是:NetBeans没有通过调用将超类字段考虑在内超类方法?
Seriously, though: what's up with NetBeans not taking superclass fields into account by calling the superclass methods?
这篇关于覆盖equals()&子类中的hashCode()...考虑超级字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!