本帖最后由 fso918 于 2011-11-1 10:37 编辑
class Student{
private id ;
private age;
public Student(int id,int age){
this.id = id;
this.age = age;
}
@Override
public int hashCode(){
return id + age;
}
@Override
public boolean equals(Object obj){
if(obj instanceof Student)
return obj.id == this.id;
return false;
}
}
class Test{
public void main(String[] args){
Student s1 = new Student(10,11);
Student s2 = new Student(10,12);
System.out.println(s1.hashCode() == s2.hashCode());
System.out.println(s1.equals(s2);
}
} |