2.某公司的雇员分为以下若干类: (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ① 属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 ① 属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2024-03-14 12:46:01 浏览: 134
以下是实现该程序的Java代码:
```java
public 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) {
double salary = 0;
return salary;
}
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
}
public 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) {
double salary = monthlySalary;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
public class HourlyEmployee extends Employee {
private double hourlySalary;
private double monthlyWorkHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, double monthlyWorkHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.monthlyWorkHours = monthlyWorkHours;
}
public double getSalary(int month) {
double salary = 0;
if (monthlyWorkHours <= 160) {
salary = monthlyWorkHours * hourlySalary;
} else {
salary = 160 * hourlySalary + (monthlyWorkHours - 160) * hourlySalary * 1.5;
}
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
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 getSalary(int month) {
double salary = monthlySales * commissionRate;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
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 getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("Tom", 2, 5000);
employees[1] = new HourlyEmployee("Jerry", 5, 100, 180);
employees[2] = new SalesEmployee("Lucy", 8, 10000, 0.05);
employees[3] = new BasePlusSalesEmployee("Bob", 10, 12000, 0.1, 5000);
employees[4] = new SalariedEmployee("Alice", 12, 6000);
int month = 8;
for (Employee employee : employees) {
System.out.println(employee.getName() + "的工资是:" + employee.getSalary(month));
}
}
}
```
代码解释如下:
1. 首先定义了一个 `Employee` 类作为所有员工的父类,包含了员工的姓名和生日月份两个属性,以及一个 `getSalary` 方法来计算月薪。
2. 然后分别定义了四个子类:`SalariedEmployee`、`HourlyEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee`,它们分别对应拿固定工资、按小时拿工资、销售和固定底薪的销售人员,这些子类都继承了 `Employee` 父类,并重写了 `getSalary` 方法来计算月薪。
3. 最后在 `EmployeeTest` 类中创建一个 `Employee` 类型的数组,分别创建了若干不同类型的 `Employee` 对象,并打印了某个月的工资。
4. 在计算月薪的过程中,如果员工的生日月份与给定的月份相同,则公司会额外奖励100元,这个逻辑被封装在 `getSalary` 方法中,实现了完全封装,不允许非私有化属性。
阅读全文