(2)某学校有3类员工:教师、行政人员、教师兼行政人员,共有的信息包括编号、姓名、性别和职工类别。工资计算方法如下。 教师:基本工资+课时数×课时补贴。 行政人员:基本工资+行政补贴。 教师兼行政人员:基本工资+课时数×课时补贴+行政补贴。 分析以上信息,定义人员抽象类,派生不同类型的员工,并完成工资的计算。
时间: 2023-06-01 07:02:19 浏览: 61
人员抽象类定义:
```java
public abstract class Employee {
private String id;
private String name;
private String gender;
private String type;
public Employee(String id, String name, String gender, String type) {
this.id = id;
this.name = name;
this.gender = gender;
this.type = type;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getType() {
return type;
}
public abstract double getSalary();
}
```
教师类:
```java
public class Teacher extends Employee {
private int hour;
private double allowancePerHour;
public Teacher(String id, String name, String gender, String type, int hour, double allowancePerHour) {
super(id, name, gender, type);
this.hour = hour;
this.allowancePerHour = allowancePerHour;
}
public int getHour() {
return hour;
}
public double getAllowancePerHour() {
return allowancePerHour;
}
@Override
public double getSalary() {
return hour * allowancePerHour;
}
}
```
行政人员类:
```java
public class Administrator extends Employee {
private double allowance;
public Administrator(String id, String name, String gender, String type, double allowance) {
super(id, name, gender, type);
this.allowance = allowance;
}
public double getAllowance() {
return allowance;
}
@Override
public double getSalary() {
return allowance;
}
}
```
教师兼行政人员类:
```java
public class TeacherAdministrator extends Employee {
private int hour;
private double allowancePerHour;
private double allowance;
public TeacherAdministrator(String id, String name, String gender, String type, int hour, double allowancePerHour, double allowance) {
super(id, name, gender, type);
this.hour = hour;
this.allowancePerHour = allowancePerHour;
this.allowance = allowance;
}
public int getHour() {
return hour;
}
public double getAllowancePerHour() {
return allowancePerHour;
}
public double getAllowance() {
return allowance;
}
@Override
public double getSalary() {
return hour * allowancePerHour + allowance;
}
}
```
使用示例:
```java
public class Test {
public static void main(String[] args) {
Employee e1 = new Teacher("001", "张三", "男", "教师", 40, 50);
Employee e2 = new Administrator("002", "李四", "女", "行政人员", 5000);
Employee e3 = new TeacherAdministrator("003", "王五", "男", "教师兼行政人员", 40, 50, 5000);
System.out.println(e1.getName() + " 的工资为:" + e1.getSalary());
System.out.println(e2.getName() + " 的工资为:" + e2.getSalary());
System.out.println(e3.getName() + " 的工资为:" + e3.getSalary());
}
}
```