编程语言 php java

java中每个Object都有一个equals方法

java HTML我帮您 1年前  0次浏览
java中每个Object都有一个equals方法,可以通过重写equals函数来实现这个功能。
public class Student {
private String gender;
private String name;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String gender, String name) {
this.gender = gender;
this.name = name;
}
public boolean equals(Object o) {
if (!(o instanceof Student))
return false;
final Student other = (Student) o;
if (getName().equals(other.getName())
& getGender().equals(other.getGender()))
return true;
else
return false;
}
}

发表评论