编程:某公司的雇员分为以下若干类:
(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会
额外奖励 100 元。
(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。
(3) HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出 160 小时的部分
按照 1.5 倍工资发放。
① 属性:每小时的工资、每月工作的小时数。
(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
① 属性:月销售额、提成率。
(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加
上销售提成部分。
① 属性:底薪。
要求:
(1) 创建 SalariedEmployee、HourlyEmployee、SaleEmployee、BasePlusSalesEmployee
四个类的对象各一个
(2) 并调用父类 getSalary(int money)方法计算某个月这四个对象各自的工资
注意:要求把每个类都做成完全封装,不允许非私有化属性。
package com.wyh;
public class TestWork26 {
public static void main(String[] args) {
SalariedEmployee salariedEmployee = new SalariedEmployee("马化腾", 2, 10000);
System.out.println("小马哥:" + salariedEmployee.getSalary(1));
System.out.println("小马哥生日:" + salariedEmployee.getSalary(2));
HourlyEmployee hourlyEmployee = new HourlyEmployee("马云", 3, 200, 172);
System.out.println("杰克马:" + hourlyEmployee.getSalary(2));
System.out.println("杰克马生日:" + hourlyEmployee.getSalary(3));
SalesEmployee salesEmployee = new SalesEmployee("雷军", 4, 100000, 0.3);
System.out.println("OK哥:" + salesEmployee.getSalary(3));
System.out.println("OK哥生日:" + salesEmployee.getSalary(4));
BasePlusSalesEmployee basePlusSalesEmployee = new BasePlusSalesEmployee("刘强东", 5, 20000, 0.03, 5000);
System.out.println("东哥:" + basePlusSalesEmployee.getSalary(4));
System.out.println("东哥生日:" + basePlusSalesEmployee.getSalary(5));
}
}
class Employee {
private String name;
private int month;
public Employee() {
}
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
public double getSalary(int month) {
if (this.month == month)
return 100;
return 0;
}
}
class SalariedEmployee extends Employee {
private double salary; //月薪
public SalariedEmployee() {
}
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public double getSalary(int month) {
return salary + super.getSalary(month);
}
}
class HourlyEmployee extends Employee {
private double hourlySalary; // 时薪
private int hours; //每月工作小时数
public HourlyEmployee() {
}
public HourlyEmployee(String name, int month, double hourlySalary, int hours) {
super(name, month);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getSalary(int month) {
if (hours > 160){
return hourlySalary * 160 + 1.5 * hourlySalary * (hours - 160) + super.getSalary(month);
}
return hourlySalary * hours + super.getSalary(month);
}
}
class SalesEmployee extends Employee {
private double sales; //月销售额
private double rate; //提成率
public SalesEmployee() {
}
public SalesEmployee(String name, int month, double sales, double rate) {
super(name, month);
this.sales = sales;
this.rate = rate;
}
public double getSalary(int month) {
return sales * rate + super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double basicSalary; //底薪
public BasePlusSalesEmployee() {
}
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double basicSalary) {
super(name, month, sales, rate);
this.basicSalary = basicSalary;
}
public double getSalary(int month) {
return basicSalary + super.getSalary(month);
}
}
运行结果: