| 
 | 
 
 
发表于 2023-10-29 17:54:13
|
显示全部楼层
 
 
 
根据题目描述,我们可以设计如下的类图: 
 
- +---------------------+
 
 - |       Employee      |
 
 - |---------------------|
 
 - | - name : String     |
 
 - | - type : String     |
 
 - |---------------------|
 
 - | + earnings() : int  |
 
 - +---------------------+
 
 -         /_\
 
 -          |
 
 -          |
 
 - +---------------------+
 
 - |  RegularEmployee    |
 
 - |---------------------|
 
 - | - monthlySalary : int|
 
 - |---------------------|
 
 - | + earnings() : int  |
 
 - +---------------------+
 
 -         /_\
 
 -          |
 
 -          |
 
 - +---------------------+
 
 - | TemporaryEmployee   |
 
 - |---------------------|
 
 - | - weeklySalary : int |
 
 - |---------------------|
 
 - | + earnings() : int  |
 
 - +---------------------+
 
 -         /_\
 
 -          |
 
 -          |
 
 - +---------------------+
 
 - |       Company       |
 
 - |---------------------|
 
 - | - employees : Employee[] |
 
 - |---------------------|
 
 - | + calculateTotalSalary() : int |
 
 - +---------------------+
 
 
  复制代码 
 
在这个类图中,Employee类是一个抽象类,包含了姓名(name)和用工类型(type)两个成员变量,以及一个抽象方法earnings()。RegularEmployee类是Employee类的子类,有一个额外的成员变量monthlySalary,实现了earnings()方法。TemporaryEmployee类也是Employee类的子类,有一个额外的成员变量weeklySalary,同样实现了earnings()方法。Company类包含一个Employee对象数组成员employees,用于存储公司的员工信息,并且有一个方法calculateTotalSalary()用于计算一年内所有员工需要支付的薪水总额。 
 
以下是相应的代码实现: 
 
- // 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 + "元");
 
 -     }
 
 - }
 
 
  复制代码 
 
运行结果: 
 
 
 
以上就是根据题目要求所画出的类图,并编写了相应的Java代码进行测试运行。 |   
 
 
 
 |