1. (考察类的继承与方法的重写)
class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public double getSalary(int month) {
if (birthMonth == month) {
return 100;
} else {
return 0;
}
}
}
class SalariedEmployee extends Employee {
private float monthSalary;
public SalariedEmployee(String name, int birthMonth, float monthSalary) {
super(name, birthMonth);
this.monthSalary = monthSalary;
}
public double getSalary(int month) {
return super.getSalary(month) + monthSalary;
}
}
class HourlyEmployee extends Employee {
private double hourSalary;
private int workHours;
public HourlyEmployee(String name, int birthMonth, double hourSalary, int workHours) {
super(name, birthMonth);
this.hourSalary = hourSalary;
this.workHours = workHours;
}
public double getSalary(int month) {
if (workHours <= 160) {
return super.getSalary(month) + hourSalary * workHours;
} else {
return super.getSalary(month) + hourSalary * workHours + 1.5 * hourSalary * (workHours - 160);
}
}
}
class SalesEmployee extends Employee {
private double monthSales;
private double royaltyRate;
public SalesEmployee(String name, int birthMonth, double monthSales, double royaltyRate) {
super(name, birthMonth);
this.monthSales = monthSales;
this.royaltyRate = royaltyRate;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + monthSales * (1 + royaltyRate);
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double basicSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double monthSales, double royaltyRate, double basicSalary) {
super(name, birthMonth, monthSales, royaltyRate);
this.basicSalary = basicSalary;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + basicSalary;
}
}
public class mm {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("李华", 8, 3000);
employees[1] = new HourlyEmployee("陈岩", 3, 30, 168);
employees[2] = new SalesEmployee("吕布", 6, 500, 0.2);
employees[3] = new BasePlusSalesEmployee("赵四", 10, 170, 0.2, 3000);
int targetMonth = 10;
for (Employee employee : employees) {
System.out.println(employee.getSalary(targetMonth));
}
}
}
分为以下若干类:
(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。
(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。
① 属性:每小时的工资、每月工作的小时数。
(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定(工资=销售额*(1+提成率))。
① 属性:月销售额、提成率。
(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分(工资=底薪+销售额*(1+提成率))。
- 属性:底薪。
要求:
在main方法中创建一个Employee 数组,分别创建若干不同子类的Employee对象,并打印某个月的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。