抽象类练习员工案例

抽象类练习员工案例

class Test03_Employee {
    public static void main(String[] args) {
        Coder c = new Coder("德玛西亚","007",8000);
        c.work();

        Manager m = new Manager("苍老师","9527",3000,20000);
        m.work();
    }
}
/*
* 假如我们在开发一个系统时需要对程序员类进行设计,程序员包含3个属性:姓名、工号以及工资。
    * 经理,除了含有程序员的属性外,另为还有一个奖金属性。
    * 请使用继承的思想设计出程序员类和经理类。要求类中提供必要的方法进行属性访问。
*/

abstract class Employee {
    private String name;                    //姓名
    private String id;                      //工号
    private double salary;                  //工资

    public Employee() {}                    //空参构造

    public Employee(String name,String id,double salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public void setName(String name) {      //设置姓名
        this.name = name;
    }

    public String getName() {               //获取姓名
        return name;
    }

    public void setId(String id) {          //设置id
        this.id = id;
    }

    public String getId() {                 //获取id
        return id;
    }

    public void setSalary(double salary) {  //设置工资
        this.salary = salary;
    }

    public double getSalary() {             //获取工资
        return salary;
    }

    public abstract void work();
}

//程序员
class Coder extends Employee {
    public Coder() {}                   //空参构造

    public Coder(String name,String id,double salary) {
        super(name,id,salary);
    }

    public void work() {
        System.out.println("我的姓名是:" + this.getName() + ",我的工号是:" + this.getId() + ",我的工资是:" 
            + this.getSalary() + ",我的工作内容是敲代码");
    }
}

//项目经理
class Manager extends Employee {
    private int bonus;                  //奖金
    public Manager() {}                 //空参构造

    public Manager(String name,String id,double salary,int bonus) {
        super(name,id,salary);
        this.bonus = bonus;
    }

    public void work() {
        System.out.println("我的姓名是:" + this.getName() + ",我的工号是:" + this.getId() + ",我的工资是:" 
            + this.getSalary() + ",我的奖金是:" + bonus + ",我的工作内容是管理");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左绍骏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值