1、重写equals,方法如下,解决类的比较:
public boolean equals(Object obj){
if(this == obj){ // 比较地址是否一样
return true;
}
if(obj == null){
return false;
}
System.out.println("this.getClass() = " + getClass() + "obj.getClass() = " + obj.getClass());
if(getClass() != obj.getClass()){ // 比较类是否相等
return false;
}
Student other = (Student)obj;
if(name == null && other.name != null){
return false;
}
if(!name.equals((other).name)){
return false;
}
return true;
}