重写equals练习

public static void main(String[] args) {
        Doctor doctor1 = new Doctor("张三", 67, "男科主任", '男', 3000);
        Doctor doctor3 = new Doctor("张三", 67, "男科主任", '男', 3000);
        Doctor doctor2 = new Doctor("李四", 37, "内科主任", '男', 5000);
        System.out.println(doctor1.equals(doctor3));
public 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;
    }
    //重写父类equals
    public boolean equals(Object obj) {
        //判断两个比较对象是否相等
        if (this == obj) {
            return true;
        }
        //判断obj 是否是 Doctor类型或是子类
        if (!(obj instanceof Doctor)) {
            return false;
        }
        //向下转型
        Doctor doctor = (Doctor)obj;
        return this.name.equals(doctor.name)
                && this.age == doctor.age
                && this.job.equals(doctor.job)
                && this.sal == doctor.sal
                && this.gender == doctor.gender;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值