java-面向对象中级-习题(通过继承实现员工工资核算打印功能 )

通过继承实现员工工资核算打印功能
父类:员工类
子类:部门经理类、普通员工类
(1)部门经理工资=1000+单日工资*天数*等级(1.2)。=>奖金+基本工资(2)普通员工工资=单日工资*天数*等级(1.0);=>基本工资
(3) 员工属性:姓名,单日工资,工作天数(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资

Homeworkmain class

package com.hspedu.homework;

public class Homework {
    public static void main(String[] args) {
        Manager manager = new Manager("刘备", 100, 20, 1.2);
        //设置奖金
        manager.setBonus(3000);
        //打印经理的工资情况
        manager.printSal();

        Worker worker = new Worker("关羽", 80, 100, 1.0);
        worker.printSal();
    }
}

Employee parent class

package com.hspedu.homework;

public class Employee {

    //属性
     private String name;
     private double daySal;
     private int workDays;
     private double grade;
    //方法[构造器]
    //打印工资的方法
    public void printSal(){
        System.out.println(name + " 工资=" + daySal * workDays * grade);
    }
    public Employee(String name, double daySal, int workDays, double grade) {
        this.name = name;
        this.daySal = daySal;
        this.workDays = workDays;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

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

    public double getDaySal() {
        return daySal;
    }

    public void setDaySal(double daySal) {
        this.daySal = daySal;
    }

    public int getWorkDays() {
        return workDays;
    }

    public void setWorkDays(int workDays) {
        this.workDays = workDays;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }
}

Manager subclass

package com.hspedu.homework;

public class Manager extends Employee{
    //特有的属性
    private double bonus;
    //奖金通过set方法设置
    public Manager(String name, double daySal, int workDays, double grade) {
        super(name, daySal, workDays, grade);
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }
    //重写工资打印方法
    @Override
    public void printSal() {
        //重写经理的工资计算方式
        System.out.println("经理 " + getName() + " 工资是=" + (bonus +
                getDaySal() * getWorkDays() * getGrade()));
    }
}

Worker subclass

package com.hspedu.homework;

public class Worker extends Employee{
    //普通员工没有特有的属性
    public Worker(String name, double daySal, int workDays, double grade) {
        super(name, daySal, workDays, grade);
    }
    //重写printSal

    @Override
    public void printSal() {
        System.out.print("普通员工 ");
        super.printSal();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值