自学内容网 自学内容网

Java继承练习

创建一个员工类(属性:名字、年龄、工作天数、日结工资,方法:打印工资),创建经理类和普通员工继承员工类且重写打印工资的方法

,经理类新增工资算法,原工资+奖金。



public class Employee {
    private String name;
    private double daySalary;
    private int workDay;
    private double grade;

    public Employee(String name, double daySalary, int workDay, double grade) {
        this.name = name;
        this.daySalary = daySalary;
        this.workDay = workDay;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

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

    public double getDaySalary() {
        return daySalary;
    }

    public void setDaySalary(double daySalary) {
        this.daySalary = daySalary;
    }

    public int getWorkDay() {
        return workDay;
    }

    public void setWorkDay(int workDay) {
        this.workDay = workDay;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }
    //打印工资方法
    public void printSalary(){
        System.out.println(name+" 的工资: "+daySalary*workDay*grade);
    }
}


public class Manager extends Employee{
    private double bonus;

    public double getBonus() {
        return bonus;
    }

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

    public Manager(String name, double daySalary, int workDay, double grade) {
        super(name, daySalary, workDay, grade);
    }

    @Override
    public void printSalary() {
        //因为经理的工资计算和员工的不一样所以要重写
        System.out.println("经理 "+getName()+"工资是: "+(bonus+getDaySalary()*getWorkDay()*getGrade()));
    }
}


public class homework03 {
    public static void main(String[] args) {
        Manager lb = new Manager("刘备",100,20,1.2);
        lb.setBonus(3000);
        lb.printSalary();
        worker zf = new worker("张飞", 80, 25, 1.0);
        zf.printSalary();
    }
}


原文地址:https://blog.csdn.net/m0_66130067/article/details/143531273

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!