+---------------------+
| Employee |
|---------------------|
| - name : String |
| - type : String |
|---------------------|
| + earnings() : int |
+---------------------+
/_\
|
|
+---------------------+
| RegularEmployee |
|---------------------|
| - monthlySalary : int|
|---------------------|
| + earnings() : int |
+---------------------+
/_\
|
|
+---------------------+
| TemporaryEmployee |
|---------------------|
| - weeklySalary : int |
|---------------------|
| + earnings() : int |
+---------------------+
/_\
|
|
+---------------------+
| Company |
|---------------------|
| - employees : Employee[] |
|---------------------|
| + calculateTotalSalary() : int |
+---------------------+
// Employee.java
public abstract class Employee {
private String name;
private String type;
public Employee(String name, String type) {
this.name = name;
this.type = type;
}
public abstract int earnings();
// Getters and setters
}
// RegularEmployee.java
public class RegularEmployee extends Employee {
private int monthlySalary;
public RegularEmployee(String name, String type, int monthlySalary) {
super(name, type);
this.monthlySalary = monthlySalary;
}
@Override
public int earnings() {
return monthlySalary * 12;
}
// Getters and setters
}
// TemporaryEmployee.java
public class TemporaryEmployee extends Employee {
private int weeklySalary;
public TemporaryEmployee(String name, String type, int weeklySalary) {
super(name, type);
this.weeklySalary = weeklySalary;
}
@Override
public int earnings() {
return weeklySalary * 52;
}
// Getters and setters
}
// Company.java
public class Company {
private Employee[] employees;
public Company(Employee[] employees) {
this.employees = employees;
}
public int calculateTotalSalary() {
int totalSalary = 0;
for (Employee employee : employees) {
totalSalary += employee.earnings();
}
return totalSalary;
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
RegularEmployee regularEmployee1 = new RegularEmployee("张三", "正式员工", 5000);
RegularEmployee regularEmployee2 = new RegularEmployee("李四", "正式员工", 5000);
RegularEmployee regularEmployee3 = new RegularEmployee("王五", "正式员工", 5000);
RegularEmployee regularEmployee4 = new RegularEmployee("赵六", "正式员工", 5000);
TemporaryEmployee temporaryEmployee1 = new TemporaryEmployee("小明", "临聘员工", 1500);
TemporaryEmployee temporaryEmployee2 = new TemporaryEmployee("小红", "临聘员工", 1500);
TemporaryEmployee temporaryEmployee3 = new TemporaryEmployee("小刚", "临聘员工", 1500);
Employee[] employees = {regularEmployee1, regularEmployee2, regularEmployee3, regularEmployee4,
temporaryEmployee1, temporaryEmployee2, temporaryEmployee3};
Company company = new Company(employees);
int totalSalary = company.calculateTotalSalary();
System.out.println("公司一年内需要支付的薪水总额为:" + totalSalary + "元");
}
}