Java中类的继承及其应用(加强1)

问题描述

1、假如我们在开发一个系统时需要对员工进行建模,【员工Employee】包含3个属性:姓名name、工号id以及工资salary;【经理Manager】也是员工,除了含有员工的属性外,另为还有一个奖金属性bonus。
请使用继承的思想设计出员工类和经理类,要求类中提供必要的方法进行属性访问,并能够一次性输出对象的基本信息。

完整代码

public class Main {
    public static void main(String[] args) {
        Employee e1 = new Employee("Zhangsan",20202001,8800);
        Manager m1 =new Manager("Lisi",20203001,9800,12000);
        e1.show();
        m1.show();

    }
    static class Employee {
        private String name;
        private int ID;
        private double salary;
        public Employee(){}
        public Employee(String name,int ID,double salary){
            this.name=name;
            this.ID=ID;
            this.salary=salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getID() {
            return ID;
        }

        public void setID(int ID) {
            this.ID = ID;
        }

        public double getSalary() {
            return salary;
        }

        public void setSalary(double salary) {
            this.salary = salary;
        }
        void show(){
            System.out.println("Name: "+name+", "+"Job number: " +ID+", "+"Salary: "+salary);
        }

    }
    static class Manager extends Employee {
        private double bonus;
        public Manager(String name,int ID,double salary,double bonus){
            super(name,ID,salary);
            this.bonus=bonus;
        }
        public double getBonus() {
            return bonus;
        }

        public void setBonus(double bonus) {
            this.bonus = bonus;
        }

        @Override
        void show() {
            System.out.println("Name: "+super.name+", "+"Job number: " +super.ID+", "+"Salary: "+super.salary+", "+"Bonus: "+bonus);
        }
    }
}

运行结果

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值