我有一个班级学校:
public class School{
private int noOfTeachers;
private int noOfStudents;
//setters and getters....
public boolean equals(Object that){
//instance of check..
return (this.noOfTeachers == ((School)that).noOfTeachers ||
this.noOfStudents== ((School)that).noOfStudents);
}
public int hashCode(){
//What goes in here??? o.O
}
}
我应该如何hashCode
为这个类进行实施?我无法想出一种既涉及哈希计算noOfTeachers
又涉及noOfStudents
哈希计算的策略。相反noOfTeachers
和noOfStudents
组合似乎违反了 和 之间的equals
合同hashCode
。