是的
时间: 2024-02-16 20:59:55 浏览: 24
好的,以下是代码实现,使用了面向对象的思想:
```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 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) {
double salary = monthlySalary;
if (month == this.birthMonth) {
salary += 100;
}
return salary;
}
}
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int monthlyWorkHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int monthlyWorkHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.monthlyWorkHours = monthlyWorkHours;
}
@Override
public double getSalary(int month) {
double salary = hourlySalary * monthlyWorkHours;
if (monthlyWorkHours > 160) {
salary += (monthlyWorkHours - 160) * hourlySalary * 0.5;
}
if (month == this.birthMonth) {
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;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == this.birthMonth) {
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;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (month == this.birthMonth) {
salary += 100;
}
return salary;
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("张三", 8, 5000);
employees[1] = new HourlyEmployee("李四", 6, 50, 180);
employees[2] = new SalesEmployee("王五", 3, 20000, 0.1);
employees[3] = new BasePlusSalesEmployee("赵六", 1, 30000, 0.08, 5000);
employees[4] = new SalariedEmployee("钱七", 9, 6000);
for (Employee employee : employees) {
System.out.println(employee.getSalary(9));
}
}
}
```
这里只是简单实现了每个员工类的构造方法和计算工资的方法,实际情况下可能还需要添加其他方法和属性。在main方法中,创建了一个包含不同类型员工的数组,并循环调用每个员工的getSalary方法来计算某个月的工资。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)