Java某公司有各类员工,销售员、计时工人、计件工人、老板,他们的薪水计算方法不同,各类员工的薪水计算方法为: 老板(Boss):固定工资 销售员(CommissionWorker):固定工资加销售提成(固定工资+销售额*提成率) 计时工人(HourlyWorker):计时工资加加班费(每小时工资*工作小时+加班小时*1.5*每小时工资) (每周超过40小时就算加班,加班工资是正常工资的1.5倍) 计件工人(PieceWorker):计件工资(计件工资*生产的产品件数) 设计一个接口,并实现该接口,计算各类员工的薪水。 要求:定义一个接口IEmployee,该接口提供指定实现该接口的类必须提供的基准成员,有getName方法和earnings方法;定义一个IEmployee类实现IEmployee接口,由于不同类型的员工计算薪水方法不同,每个派生类重写该方法,以实现各自的计算
时间: 2024-02-20 08:59:47 浏览: 39
下面是实现该接口的代码示例:
```java
public interface IEmployee {
public String getName();
public double earnings();
}
public class Boss implements IEmployee {
private String name;
private double salary; // 固定工资
public Boss(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double earnings() {
return salary;
}
}
public class CommissionWorker implements IEmployee {
private String name;
private double salary; // 固定工资
private double commissionRate; // 提成率
private double sales; // 销售额
public CommissionWorker(String name, double salary, double commissionRate, double sales) {
this.name = name;
this.salary = salary;
this.commissionRate = commissionRate;
this.sales = sales;
}
public String getName() {
return name;
}
public double earnings() {
return salary + sales * commissionRate;
}
}
public class HourlyWorker implements IEmployee {
private String name;
private double wage; // 每小时工资
private double hours; // 工作小时数
private double overtimeHours; // 加班小时数
public HourlyWorker(String name, double wage, double hours, double overtimeHours) {
this.name = name;
this.wage = wage;
this.hours = hours;
this.overtimeHours = overtimeHours;
}
public String getName() {
return name;
}
public double earnings() {
if (hours > 40) {
return 40 * wage + (hours - 40) * 1.5 * wage + overtimeHours * 1.5 * wage;
} else {
return hours * wage + overtimeHours * 1.5 * wage;
}
}
}
public class PieceWorker implements IEmployee {
private String name;
private double wagePerPiece; // 每件工资
private int pieces; // 生产的产品件数
public PieceWorker(String name, double wagePerPiece, int pieces) {
this.name = name;
this.wagePerPiece = wagePerPiece;
this.pieces = pieces;
}
public String getName() {
return name;
}
public double earnings() {
return wagePerPiece * pieces;
}
}
```
在上述代码中,我们定义了一个接口 `IEmployee`,它有两个方法:`getName()` 和 `earnings()`。接着,我们定义了四个类 `Boss`、`CommissionWorker`、`HourlyWorker` 和 `PieceWorker`,它们均实现了这个接口,并分别实现了各自的薪水计算方法。在每个类中,我们先实现 `getName()` 方法,返回该员工的姓名,然后实现 `earnings()` 方法,根据员工类型不同,分别计算出该员工的薪水,并返回计算结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)