编程,每类员工都有相应的封装性

img

编程,每类员工都有相应的封装性,五类成员的信息如下:每个类都完全封装,不允许有非私有化属性。

私有化属性使用 private 关键字。
父类 Employee:

public class Employee {
    //姓名
    private String name;
    //生日月份
    private int birthMonth;

    double getSalary(int month) {
        if (month == getBirthMonth()) {
            //生日月份加100
            return 100;
        } else {
            return 0;
        }
    }

    public Employee(String name, int birthMonth) {
        this.name = name;
        this.birthMonth = birthMonth;
    }

    public String getName() {
        return name;
    }

    public int getBirthMonth() {
        return birthMonth;
    }
}

SalariedEmployee:

public class SalariedEmployee extends Employee {
    //月薪
    private double monthlySalary;

    public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
        super(name, birthMonth);
        this.monthlySalary = monthlySalary;
    }

    public double getMonthlySalary() {
        return monthlySalary;
    }

    @Override
    public double getSalary(int month) {
        return monthlySalary + super.getSalary(month);
    }
}

HourlyEmployee:

public class HourlyEmployee extends Employee{
    //每小时工资
    private double hourlySalary;
    //每月工作小时数
    private double hoursPerMonth;

    public HourlyEmployee(String name, int birthMonth, double hourlySalary, double hours) {
        super(name, birthMonth);
        this.hourlySalary = hourlySalary;
        this.hoursPerMonth = hours;
    }

    public double getHourlySalary() {
        return hourlySalary;
    }

    public double getHoursPerMonth() {
        return hoursPerMonth;
    }

    @Override
    public double getSalary(int month) {
        //超出160小时的部分1.5倍工资
        return hourlySalary * (hoursPerMonth > 160 ? 160 + (hoursPerMonth - 160) * 1.5 : hoursPerMonth) + super.getSalary(month);
    }
}

SalesEmployee:

public class SalesEmployee extends Employee{
    //月销售额
    private double monthlySales;
    //提成率
    private double commissionRate;

    public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
        super(name, birthMonth);
        this.monthlySales = monthlySales;
        this.commissionRate = commissionRate;
    }

    public double getMonthlySales() {
        return monthlySales;
    }

    public double getCommissionRate() {
        return commissionRate;
    }

    @Override
    public double getSalary(int month) {
        return monthlySales * commissionRate + super.getSalary(month);
    }
}

BasePlusSalesEmployee:

public class BasePlusSalesEmployee extends SalesEmployee {
    //底薪
    private double baseSalary;

    public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) {
        super(name, birthMonth, monthlySales, commissionRate);
        this.baseSalary = baseSalary;
    }

    public double getBaseSalary() {
        return baseSalary;
    }

    @Override
    public double getSalary(int month) {
        return baseSalary + super.getSalary(month);
    }
}

EmployeeTest:

public class EmployeeTest {
    public static void main(String[] args) {
        Employee[] employees = new Employee[4];
        employees[0] = new SalariedEmployee("张三", 1, 10000);
        employees[1] = new HourlyEmployee("李四", 2, 100, 200);
        employees[2] = new SalesEmployee("王五", 3, 100000, 0.1);
        employees[3] = new BasePlusSalesEmployee("赵六", 4, 100000, 0.1, 3000);
        //输出2月份每个员工的工资
        for (Employee employee : employees) {
            System.out.println(employee.getName() + "在2月的工资为:" + employee.getSalary(2));
        }
    }
}

输出:
张三在2月的工资为:10000.0
李四在2月的工资为:22100.0
王五在2月的工资为:10000.0
赵六在2月的工资为:13000.0