某学校教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元:副教授的固定工资为 3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师子类,编写程序求若干教师的月工资
时间: 2024-12-09 15:28:57 浏览: 32
为了实现这个需求,我们可以定义一个名为`Teacher`的抽象类,包含固定的`salary`属性和一个计算月工资的方法`calculateMonthlySalary`。然后分别创建`Professor`, `AssociateProfessor`, 和 `AssistantProfessor` 这些具体的子类,并覆盖父类的计算方法,反映各自的固定工资和课时补贴。
首先,定义`Teacher`类:
```java
public abstract class Teacher {
private int fixedSalary;
private double hourBonus;
public Teacher(int fixedSalary, double hourBonus) {
this.fixedSalary = fixedSalary;
this.hourBonus = hourBonus;
}
// 抽象方法,需要子类提供实现
public abstract double calculateHourlyBonus();
// 计算月工资的方法
public double calculateMonthlySalary(int teachingHours) {
return fixedSalary + teachingHours * calculateHourlyBonus();
}
}
```
接着,创建子类并实现`calculateHourlyBonus()`方法:
```java
public class Professor extends Teacher {
public Professor() {
super(5000, 50);
}
@Override
public double calculateHourlyBonus() {
return 50;
}
}
public class AssociateProfessor extends Teacher {
public AssociateProfessor() {
super(3000, 30);
}
@Override
public double calculateHourlyBonus() {
return 30;
}
}
public class AssistantProfessor extends Teacher {
public AssistantProfessor() {
super(2000, 20);
}
@Override
public double calculateHourlyBonus() {
return 20;
}
}
```
现在你可以实例化这些教师,并调用他们的`calculateMonthlySalary`方法来获取工资,比如:
```java
public static void main(String[] args) {
Professor professor = new Professor();
double profSalary = professor.calculateMonthlySalary(20); // 教授20课时的工资
AssociateProfessor associateProfessor = new AssociateProfessor();
double assocSalary = associateProfessor.calculateMonthlySalary(30); // 副教授30课时的工资
AssistantProfessor assistantProfessor = new AssistantProfessor();
double asstSalary = assistantProfessor.calculateMonthlySalary(40); // 讲师40课时的工资
System.out.println("Professor salary: " + profSalary);
System.out.println("Associate Professor salary: " + assocSalary);
System.out.println("Assistant Professor salary: " + asstSalary);
}
```
阅读全文