栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
        }
    }
}
运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号