可以写成代码给我看一下嘛
时间: 2023-12-20 22:05:27 浏览: 26
好的,下面是一个简单的实现代码,希望能帮到你。
```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) {
return 0.0; // 默认工资为0
}
}
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
@Override
public double getSalary(int month) {
if (month == getBirthMonth()) {
return monthlySalary + 100; // 生日奖励100元
} else {
return monthlySalary;
}
}
}
public class HourlyEmployee extends Employee {
private double hourlyWage;
private int monthlyWorkHours;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, int monthlyWorkHours) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.monthlyWorkHours = monthlyWorkHours;
}
@Override
public double getSalary(int month) {
double salary = hourlyWage * monthlyWorkHours;
if (monthlyWorkHours > 160) {
salary += hourlyWage * 1.5 * (monthlyWorkHours - 160);
}
if (month == getBirthMonth()) {
salary += 100; // 生日奖励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;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == getBirthMonth()) {
salary += 100; // 生日奖励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;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (month == getBirthMonth()) {
salary += 100; // 生日奖励100元
}
return salary;
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = new SalariedEmployee("张三", 3, 5000);
employees[1] = new HourlyEmployee("李四", 6, 50, 170);
employees[2] = new BasePlusSalesEmployee("王五", 9, 100000, 0.05, 5000);
int month = 9; // 假设计算9月份的工资
for (Employee employee : employees) {
System.out.println(employee.getSalary(month));
}
}
}
```