我创建了一个具有 name 和 employeeNo 的 Employee 类。我正在覆盖 equals 和 hashcode 方法。
@Override
public boolean equals(Object obj) {
final Employee other = (Employee) obj;
if (this.employeeNo == other.employeeNo && this.name.equals(other.name)) {
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 53 * hash + this.employeeNo;
return hash;
}
我的测试班
Employee p = new Employee();
p.setName("v");
p.setEmployeeNo(1);
Employee p1 = new Employee();
p.setName("v");
p.setEmployeeNo(1);
System.out.println(p==p1); // Print false . why?