感觉比较难理解toString方法和equals方法。
比如用快捷键生成的toString和equals。
public String toString() { //创建toString方法
return "Student [name=" + name + ", gender=" + gender + ", age=" + age
+ ", score=" + score + "]";
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (score != other.score)
return false;
return true;
|
|