题目:设计一个简单的人事管理系统,其中包括以下类: (1)员工类(Employee),包含员工编号、姓名、性别、年龄、工资等属性,以及计算工资(calculateSalary)和显示信息(showInfo)两个抽象方法。 (2)经理类(Manager),是员工类的子类,增加了部门(department)属性,以及计算工资(calculateSalary)和显示信息(showInfo)两个方法的具体实现。 (3)工人类(Worker),是员工类的子类,增加了工种(job)属性,以及计算工资(calculateSalary)和显示信息(showInfo)两个方法的具体实现。 (4)财务部门接口(FinanceDepartment),包含计算税收(calculateTax)的抽象方法。 (5)财务部门类(Finance),实现了财务部门接口(FinanceDepartment),并实现了计算税收(calculateTax)的具体方法。 请根据以上要求,设计出相应的类及其关系,并实现相应的方法。
时间: 2024-01-22 08:01:52 浏览: 29
根据题目要求,我们可以设计出以下类及其关系:
1. 员工类(Employee)
- 属性:员工编号、姓名、性别、年龄、工资
- 方法:抽象方法 calculateSalary() 和 showInfo()
2. 经理类(Manager)
- 属性:继承自员工类,增加了部门(department)属性
- 方法:继承自员工类,实现 calculateSalary() 和 showInfo() 方法
3. 工人类(Worker)
- 属性:继承自员工类,增加了工种(job)属性
- 方法:继承自员工类,实现 calculateSalary() 和 showInfo() 方法
4. 财务部门接口(FinanceDepartment)
- 方法:抽象方法 calculateTax()
5. 财务部门类(Finance)
- 方法:实现 FinanceDepartment 接口,实现 calculateTax() 方法
下面是相应的 Java 代码实现:
```java
// 员工类
public abstract class Employee {
protected String id; // 员工编号
protected String name; // 姓名
protected String gender; // 性别
protected int age; // 年龄
protected double salary; // 工资
public Employee(String id, String name, String gender, int age, double salary) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.salary = salary;
}
public abstract double calculateSalary(); // 计算工资
public abstract void showInfo(); // 显示信息
}
// 经理类
public class Manager extends Employee {
private String department; // 部门
public Manager(String id, String name, String gender, int age, double salary, String department) {
super(id, name, gender, age, salary);
this.department = department;
}
@Override
public double calculateSalary() {
// 计算经理的工资
return this.salary * 1.5;
}
@Override
public void showInfo() {
System.out.println("编号:" + this.id);
System.out.println("姓名:" + this.name);
System.out.println("性别:" + this.gender);
System.out.println("年龄:" + this.age);
System.out.println("工资:" + this.salary);
System.out.println("部门:" + this.department);
}
}
// 工人类
public class Worker extends Employee {
private String job; // 工种
public Worker(String id, String name, String gender, int age, double salary, String job) {
super(id, name, gender, age, salary);
this.job = job;
}
@Override
public double calculateSalary() {
// 计算工人的工资
return this.salary * 1.2;
}
@Override
public void showInfo() {
System.out.println("编号:" + this.id);
System.out.println("姓名:" + this.name);
System.out.println("性别:" + this.gender);
System.out.println("年龄:" + this.age);
System.out.println("工资:" + this.salary);
System.out.println("工种:" + this.job);
}
}
// 财务部门接口
public interface FinanceDepartment {
public abstract double calculateTax(); // 计算税收
}
// 财务部门类
public class Finance implements FinanceDepartment {
private double income; // 收入
public Finance(double income) {
this.income = income;
}
@Override
public double calculateTax() {
// 计算税收,假设税收为收入的 10%
return this.income * 0.1;
}
}
```