
package com.lmdedu.homework;
public class Homework10 {
public static void main(String[] args) {
Doctor doctor = new Doctor("lmd", 20, "医生", '男', 50000);
Doctor doctor1 = new Doctor("lmd", 20, "医生", '男', 50000);
System.out.println(doctor.equals(doctor1));
}
}
class Doctor{
private String name;
private int age;
private String job;
private char gender;
private double sal;
public Doctor(String name, int age, String job, char gender, double 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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public boolean equals(Object obj) {
//判断两个对象是否相同
if(this == obj){
return true;
}
//判断obj是否时 Doctor类对象或其子类对象
//过关斩将 校验方式
if(!(obj instanceof Doctor)) {
return false;
}
//因为obj的运行类型是Doctor或其子类,向下转型,将Obj向下转型为Doctor,
Doctor doc = (Doctor)obj;
return this.name.equals(doc.getName()) && this.age == doc.getAge() &&
this.job.equals(doc.getJob()) && this.gender == doc.getGender()
&& this.sal == doc.getSal();
}
}
该篇文章展示了如何在Java中定义和使用一个名为Doctor的类,重点讲解了equals方法的重写,用于比较两个Doctor对象是否相等。
4227

被折叠的 条评论
为什么被折叠?



