某公司的雇员分为5类,每类员工都有相应的封装类,这5个类的信息如下所示。 (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ① 属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 ① 属性:底薪。 本题要求根据上述雇员分类,编写一个程序,实现以下功能: (1)创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。 (2)每个类都完全封装,不允许非私有化属性。(1)编写应用程序,创建一个Employee 数组,分别创建若干不同的Employee对象,并打印员工姓名、生日月份和某个月的工资。(在实验十七基础上完成)
时间: 2023-06-24 07:06:57 浏览: 2245
以下是实现上述功能的代码:
```java
public class EmployeeTest {
public static void main(String[] args) {
Employee[] staff = new Employee[5];
staff[0] = new SalariedEmployee("John", 8, 2000);
staff[1] = new HourlyEmployee("Mary", 12, 160, 30);
staff[2] = new SalesEmployee("Tom", 9, 5000, 0.05);
staff[3] = new BasePlusSalesEmployee("Jerry", 11, 3000, 0.1, 1000);
staff[4] = new SalariedEmployee("Lisa", 6, 2500);
int month = 12;
for (Employee e : staff) {
System.out.println("Name: " + e.getName() + ", Birth Month: " + e.getBirthMonth() + ", Salary: " + e.getSalary(month));
}
}
}
class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
public double getSalary(int month) {
return 0;
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getSalary(int month) {
if (month == super.getBirthMonth()) {
return monthlySalary + 100;
} else {
return monthlySalary;
}
}
}
class HourlyEmployee extends Employee {
private double hourlySalary;
private int workedHours;
private double overtimeRate;
public HourlyEmployee(String name, int birthMonth, int workedHours, double hourlySalary) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.workedHours = workedHours;
this.overtimeRate = 1.5;
}
public double getSalary(int month) {
double baseSalary = hourlySalary * workedHours;
if (workedHours > 160) {
baseSalary += (workedHours - 160) * overtimeRate * hourlySalary;
}
return baseSalary;
}
}
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 getSalary(int month) {
return monthlySales * commissionRate;
}
}
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 getSalary(int month) {
return super.getSalary(month) + baseSalary;
}
}
```
输出结果如下:
```
Name: John, Birth Month: 8, Salary: 2000.0
Name: Mary, Birth Month: 12, Salary: 4800.0
Name: Tom, Birth Month: 9, Salary: 250.0
Name: Jerry, Birth Month: 11, Salary: 1300.0
Name: Lisa, Birth Month: 6, Salary: 2500.0
```
可以看到,根据不同的 Employee 子类对象,正确计算出了每个员工在指定月份的工资,并打印了员工的姓名和生日月份。同时,每个子类都封装了自己的属性和计算工资的方法,并且通过继承和重写父类方法来实现了不同的工资计算方式。
阅读全文