本帖最后由 姜志钦 于 2012-3-29 11:15 编辑
重写object类中的equals方法
public class Student {
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.name = name;
this.age = age;
this.id = id;
}
@Override //equals比较对象各个属性
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name)){
return false;
}
return true;
} |