设计一个人员类(Person),包含姓名,方法pay()代表人员的工资支出;方法toString()输出人员的信息。从Person类派生出教师类(Teacher)和大学生类(CollegeStudent),其中:教师的工资支出=基本工资+授课时数*30,大学生的工资支出=奖学金支出。 增加行政员工类(AdministrativeStaff),行政员工的工资支出=基础工资+考核奖励(优秀5000,良好3000,合格1000)。 (1)将人员类Person定义为抽象类,pay()为抽象方法,设计程序实现多态性。 (2)定义一接口,将pay定义为其抽象方法,设计程序实现多态性。
时间: 2023-05-18 14:04:04 浏览: 157
1. 设计一个人员类(Person),包含姓名,方法pay()代表人员的工资支出;方法toString()输出人员的信息。从Person类派生出教师类(Teacher)和大学生类(CollegeStudent),其中:教师的工资支出=基本工资 授课时数*30,大学生的工资支出=奖学金支出。 增加行政员工类(AdministrativeStaff),行政员工的工资支出=基础工资 考核奖励(优秀5000,良好3000,合格1000)。
(1) 将人员类Person定义为抽象类,pay()为抽象方法,设计程序实现多态性。
抽象类Person的定义如下:
```
public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract double pay();
public String toString() {
return "Name: " + name + ", Pay: " + pay();
}
}
```
教师类Teacher的定义如下:
```
public class Teacher extends Person {
private double basicSalary;
private int teachingHours;
public Teacher(String name, double basicSalary, int teachingHours) {
super(name);
this.basicSalary = basicSalary;
this.teachingHours = teachingHours;
}
public double pay() {
return basicSalary + teachingHours * 30;
}
}
```
大学生类CollegeStudent的定义如下:
```
public class CollegeStudent extends Person {
private double scholarship;
public CollegeStudent(String name, double scholarship) {
super(name);
this.scholarship = scholarship;
}
public double pay() {
return scholarship;
}
}
```
行政员工类AdministrativeStaff的定义如下:
```
public class AdministrativeStaff extends Person {
private double basicSalary;
private String assessment;
public AdministrativeStaff(String name, double basicSalary, String assessment) {
super(name);
this.basicSalary = basicSalary;
this.assessment = assessment;
}
public double pay() {
double bonus = 0;
if (assessment.equals("优秀")) {
bonus = 5000;
} else if (assessment.equals("良好")) {
bonus = 3000;
} else if (assessment.equals("合格")) {
bonus = 1000;
}
return basicSalary + bonus;
}
}
```
(2) 定义一接口,将pay定义为其抽象方法,设计程序实现多态性。
定义一个接口Payable,将pay定义为其抽象方法,Person、Teacher、CollegeStudent和AdministrativeStaff都实现Payable接口,实现多态性。
Payable接口的定义如下:
```
public interface Payable {
public abstract double pay();
}
```
Person类的定义如下:
```
public abstract class Person implements Payable {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return "Name: " + name + ", Pay: " + pay();
}
}
```
教师类Teacher的定义如下:
```
public class Teacher extends Person {
private double basicSalary;
private int teachingHours;
public Teacher(String name, double basicSalary, int teachingHours) {
super(name);
this.basicSalary = basicSalary;
this.teachingHours = teachingHours;
}
public double pay() {
return basicSalary + teachingHours * 30;
}
}
```
大学生类CollegeStudent的定义如下:
```
public class CollegeStudent extends Person {
private double scholarship;
public CollegeStudent(String name, double scholarship) {
super(name);
this.scholarship = scholarship;
}
public double pay() {
return scholarship;
}
}
```
行政员工类AdministrativeStaff的定义如下:
```
public class AdministrativeStaff extends Person {
private double basicSalary;
private String assessment;
public AdministrativeStaff(String name, double basicSalary, String assessment) {
super(name);
this.basicSalary = basicSalary;
this.assessment = assessment;
}
public double pay() {
double bonus = 0;
if (assessment.equals("优秀")) {
bonus = 5000;
} else if (assessment.equals("良好")) {
bonus = 3000;
} else if (assessment.equals("合格")) {
bonus = 1000;
}
return basicSalary + bonus;
}
}
```
阅读全文