鱼C论坛

 找回密码
 立即注册
查看: 2802|回复: 2

4.这个题的类图怎么画?

[复制链接]
发表于 2023-10-29 17:54:13 | 显示全部楼层
根据题目描述,我们可以设计如下的类图:

  1. +---------------------+
  2. |       Employee      |
  3. |---------------------|
  4. | - name : String     |
  5. | - type : String     |
  6. |---------------------|
  7. | + earnings() : int  |
  8. +---------------------+
  9.         /_\
  10.          |
  11.          |
  12. +---------------------+
  13. |  RegularEmployee    |
  14. |---------------------|
  15. | - monthlySalary : int|
  16. |---------------------|
  17. | + earnings() : int  |
  18. +---------------------+
  19.         /_\
  20.          |
  21.          |
  22. +---------------------+
  23. | TemporaryEmployee   |
  24. |---------------------|
  25. | - weeklySalary : int |
  26. |---------------------|
  27. | + earnings() : int  |
  28. +---------------------+
  29.         /_\
  30.          |
  31.          |
  32. +---------------------+
  33. |       Company       |
  34. |---------------------|
  35. | - employees : Employee[] |
  36. |---------------------|
  37. | + calculateTotalSalary() : int |
  38. +---------------------+
复制代码


在这个类图中,Employee类是一个抽象类,包含了姓名(name)和用工类型(type)两个成员变量,以及一个抽象方法earnings()。RegularEmployee类是Employee类的子类,有一个额外的成员变量monthlySalary,实现了earnings()方法。TemporaryEmployee类也是Employee类的子类,有一个额外的成员变量weeklySalary,同样实现了earnings()方法。Company类包含一个Employee对象数组成员employees,用于存储公司的员工信息,并且有一个方法calculateTotalSalary()用于计算一年内所有员工需要支付的薪水总额。

以下是相应的代码实现:

  1. // Employee.java
  2. public abstract class Employee {
  3.     private String name;
  4.     private String type;

  5.     public Employee(String name, String type) {
  6.         this.name = name;
  7.         this.type = type;
  8.     }

  9.     public abstract int earnings();

  10.     // Getters and setters
  11. }

  12. // RegularEmployee.java
  13. public class RegularEmployee extends Employee {
  14.     private int monthlySalary;

  15.     public RegularEmployee(String name, String type, int monthlySalary) {
  16.         super(name, type);
  17.         this.monthlySalary = monthlySalary;
  18.     }

  19.     @Override
  20.     public int earnings() {
  21.         return monthlySalary * 12;
  22.     }

  23.     // Getters and setters
  24. }

  25. // TemporaryEmployee.java
  26. public class TemporaryEmployee extends Employee {
  27.     private int weeklySalary;

  28.     public TemporaryEmployee(String name, String type, int weeklySalary) {
  29.         super(name, type);
  30.         this.weeklySalary = weeklySalary;
  31.     }

  32.     @Override
  33.     public int earnings() {
  34.         return weeklySalary * 52;
  35.     }

  36.     // Getters and setters
  37. }

  38. // Company.java
  39. public class Company {
  40.     private Employee[] employees;

  41.     public Company(Employee[] employees) {
  42.         this.employees = employees;
  43.     }

  44.     public int calculateTotalSalary() {
  45.         int totalSalary = 0;
  46.         for (Employee employee : employees) {
  47.             totalSalary += employee.earnings();
  48.         }
  49.         return totalSalary;
  50.     }
  51. }

  52. // 测试代码
  53. public class Main {
  54.     public static void main(String[] args) {
  55.         RegularEmployee regularEmployee1 = new RegularEmployee("张三", "正式员工", 5000);
  56.         RegularEmployee regularEmployee2 = new RegularEmployee("李四", "正式员工", 5000);
  57.         RegularEmployee regularEmployee3 = new RegularEmployee("王五", "正式员工", 5000);
  58.         RegularEmployee regularEmployee4 = new RegularEmployee("赵六", "正式员工", 5000);

  59.         TemporaryEmployee temporaryEmployee1 = new TemporaryEmployee("小明", "临聘员工", 1500);
  60.         TemporaryEmployee temporaryEmployee2 = new TemporaryEmployee("小红", "临聘员工", 1500);
  61.         TemporaryEmployee temporaryEmployee3 = new TemporaryEmployee("小刚", "临聘员工", 1500);

  62.         Employee[] employees = {regularEmployee1, regularEmployee2, regularEmployee3, regularEmployee4,
  63.                 temporaryEmployee1, temporaryEmployee2, temporaryEmployee3};

  64.         Company company = new Company(employees);
  65.         int totalSalary = company.calculateTotalSalary();
  66.         System.out.println("公司一年内需要支付的薪水总额为:" + totalSalary + "元");
  67.     }
  68. }
复制代码


运行结果:

  1. 公司一年内需要支付的薪水总额为:122400元
复制代码


以上就是根据题目要求所画出的类图,并编写了相应的Java代码进行测试运行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-4 08:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表