EmployeeTest

定义一个Employee类继承于Person类,包含新的属性:职位;写好相关的方法,能够构造的时候初始化或者后续修改属性值,重写equals方法,比较规则:当两个对象的姓名,年龄,性别,职位都相等时返回true,否则返回false。


/*
 *写一个测试类进行相关测试(要求可以打印对象的信息,能够进行对象的比较是否"相等")。
 */
public class EmployeeTestDemo {

    public static void main(String[] args){
        Employee employee1=new Employee("Jim",28,'男',"软件工程师");

        Employee employee2=new Employee("Tom",28,'男',"软件工程师");
        employee1.getDetail();
        employee2.getDetail();
        System.out.println("判断两位职员是否“相等”:");
        System.out.println(employee1.equals(employee2));



    }
}

public class Person {

    private String name;//姓名

    private int age;//年龄
    private char sex;//性别
    public Person(String name,int age,char sex){
        this.setName(name);
        this.setAge(age);
        this.setSex(sex);
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public char getSex() {
        return sex;
    }


}

public class Employee extends Person{
    private String position;//职位
    public Employee(String name,int age,char sex,String position){
        super(name, age, sex);
        this.setPosition(position);


    }
    public void setPosition(String position) {
        this.position = position;
    }
    public String getPosition() {
        return position;
    }
    //重写方法,判断对象是否相等
    public boolean equals(Employee b){
        if(this.getName()==b.getName()&&this.getAge()==b.getAge()
                &&this.getSex()==b.getSex()&&this.getPosition()==b.getPosition()){
            return true;
        }
        else{
            return false;
        }
    }
    //输出对象的信息
    public void getDetail(){
        System.out.println("name: "+this.getName()+"age: "+this.getAge()+"sex: "+this.getSex()+"position: "+this.getPosition());

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值