Có một quy tắc cụ thể về cách Overriding equals()
& hashCode()
trong lớp tiểu xem xét siêu lĩnh vực ?? biết rằng có nhiều thông số: siêu trường là private/public, có/không có getter ...bình đẳng Overriding() & hashCode() trong các lớp học phụ ... xem xét siêu lĩnh vực
Ví dụ, Netbeans tạo equals() & hashCode() sẽ không xem xét các lĩnh vực siêu ... và
new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals(
new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot"))
sẽ trở thành sự thật :(
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;
}
...
}
Nếu bạn muốn xem Netbeans tạo 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;
}
}
@CPerkins: thật khó để tự động hóa thế hệ này, hãy nghĩ về trường hợp của các trường riêng có/không có getter ... Tôi nghĩ đó là lý do tại sao IDE không làm ... –
@wj, tất cả những gì bạn phải làm là gọi superclass bằng và phương thức hashcode, và các trường được tính đến. IDE có thể dễ dàng làm điều này. Trẻ em không cần phải kiểm tra một cách rõ ràng các thành viên riêng của cha mẹ. – CPerkins
@CPerkins, bạn nói đúng !! –