某企业对员工每月工资的计算规定如下:固定工资+绩校补贴。 高级工程师的固定工资为5000元,每次加班补贴50元。 初级工程师的固定工资为3000元,每次加班补贴30元。 学员的固定工资为2000元,每次加班补贴20元。 要求:定义员工抽象类,派生不同级别的员工类,用java编写程序求若干个员工的月工资。
时间: 2024-03-23 14:41:29 浏览: 14
好的,可以使用以下代码实现。首先我们定义一个员工抽象类,并在其中定义了一个计算月工资的抽象方法:
```java
public abstract class Employee {
protected String name; // 姓名
protected double fixedSalary; // 固定工资
public Employee(String name, double fixedSalary) {
this.name = name;
this.fixedSalary = fixedSalary;
}
// 计算月工资的抽象方法
public abstract double calculateSalary(int overtime);
}
```
然后我们派生出不同级别的员工类:
```java
public class SeniorEngineer extends Employee {
private static final double OVERTIME_SUBSIDY = 50.0;
public SeniorEngineer(String name) {
super(name, 5000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
public class JuniorEngineer extends Employee {
private static final double OVERTIME_SUBSIDY = 30.0;
public JuniorEngineer(String name) {
super(name, 3000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
public class Trainee extends Employee {
private static final double OVERTIME_SUBSIDY = 20.0;
public Trainee(String name) {
super(name, 2000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
```
最后我们使用这些类计算员工的月工资:
```java
public class Main {
public static void main(String[] args) {
Employee[] employees = {
new SeniorEngineer("张三"),
new JuniorEngineer("李四"),
new Trainee("王五")
};
int[] overtimes = {10, 20, 30};
for (int i = 0; i < employees.length; i++) {
double salary = employees[i].calculateSalary(overtimes[i]);
System.out.println(String.format("%s的月工资为:%.2f元", employees[i].name, salary));
}
}
}
```
这段代码中创建了三个不同级别的员工,并且分别计算了他们在不同加班时长下的月工资。运行结果如下:
```
张三的月工资为:5500.00元
李四的月工资为:3600.00元
王五的月工资为:2600.00元
```