不太记得重写equals了
public class Doctor {private String name;
private int age;
private String job;
private String gender;
private String sal;
public Doctor(String name, int age, String job, String gender, String sal) {
this.name = name;
this.age = age;
this.job = job;
this.gender = gender;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Doctor)) {
return false;
}
Doctor d = (Doctor) obj;
return this.name.equals(d.name) && this.age == d.age
&& this.gender.equals(d.gender) && this.job.equals(d.job) && this.sal == d.sal;
}
}
这里面的向下转型之后输出了那个return是啥意思,之前记得听清楚的,现在就忘了,为什么这里判断的就是属性相同呢 本帖最后由 傻眼貓咪 于 2022-6-22 22:24 编辑
你的代码最下面不是都 return 所有属性判断了吗?
equals 只是用来比较两个字符串是否相同:
"字符串1".equals("字符串2")
你的代码重写 equals 返回比较所有属性(包括 age 是整型)
页:
[1]