把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);}和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; }}和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)); } }}合成一个程序
时间: 2024-02-14 17:21:16 浏览: 241
详谈PHP中public,private,protected,abstract等关键字的用法
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);
}
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;
}
}
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));
}
}
}
阅读全文