程序首先定义父类员工类Employee,类内包含基本属性以及接口类PerformanceCalculation,包含计算效绩方法和显示工作形式方法。再依次定义其子类Teacher和ResearchStaff、LaboratoryAssistant类,然后定义Teacher和ResearchStaff的子类,追加实现计算效绩的功能。
接着定义一个关键的管理类PerformanceCalculationAdmin,内部设计绩效计算函数:通过循环计算所有员工的绩效,并打印员工信息。
在主测试类中,创建管理类对象admin和三类员工子类的对象,并分别对三个类别初始化,然后创建PerformanceCalculation对象数组和Employee对象数组,将三个类别的对象作为PerformanceCalculation对象数组成员赋值,此时我们可以确定两个数组的个数是一样的。最后调用allPerformanceCalculation计算绩效和显示员工信息。
package EXP2020.exp5;
/**
* @author JMChen
* @date 2020/4/8
*/
import java.util.Scanner;
/**
* 定义一个员工类Employee
*/
class Employee {
protected String number; //工号
protected String name; //姓名
public int sex; //性别
public String professionalTitles; //职称
protected String birthday; //生日
}
/**
* 定义员工的子类教师类Teacher
*/
class Teacher extends Employee {
public double teachingLoad; //教学工作量
public double researchLoad; //科研工作量
//定义教学方法函数
public void doTeaching() {
System.out.println("教师" + name + "正在教学,教学工作量为:" + teachingLoad);
}
//定义做科研方法函数
public void doReSearch() {
System.out.println("教师" + name + "正在做科研,科研工作量为:" + researchLoad);
}
//重写showWork,显示工作形式
public void showWork() {
doTeaching();
doReSearch();
}
}
/**
* 定义员工的子类科研人员ResearchStaff
*/
class ResearchStaff extends Employee {
public double researchLoad; //科研工作量
//定义做科研方法函数
public void doReSearch() {
System.out.println("科研人员" + name + "正在做科研,科研工作量为:" + researchLoad);
}
//重写showWork,显示工作形式
public void showWork() {
doReSearch();
}
}
/**
* 定义绩效接口PerformanceCalculation
*/
interface PerformanceCalculation {
double doPerformanceCalculation(); //计算绩效
void showWork(); //显示工作形式
}
/**
* 定义教师类的子类TeacherPeople实现接口函数
*/
class TeacherPeople extends Teacher implements PerformanceCalculation {
//计算各自的绩效
public double doPerformanceCalculation() {
return (teachingLoad - 320) / 320 + (researchLoad - 75) / 75;
}
//定义构造函数初始化
TeacherPeople(String number, String name, int sex, String birthday, String professionalTitles, double teachingLoad, double researchLoad) {
this.number = number;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.professionalTitles = professionalTitles;
this.teachingLoad = teachingLoad;
this.researchLoad = researchLoad;
}
}
/**
* 定义科研人员类的子类ResearchStaffPeople实现接口函数
*/
class ResearchStaffPeople extends ResearchStaff implements PerformanceCalculation {
//计算各自的绩效
public double doPerformanceCalculation() {
return (researchLoad - 400) / 400;
}
//定义构造函数初始化
ResearchStaffPeople(String number, String name, int sex, String birthday, String professionalTitles, double researchLoad) {
this.number = number;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.professionalTitles = professionalTitles;
this.researchLoad = researchLoad;
}
}
/**
* 定义绩效管理系统类PerformanceCalculationAdmin
*/
class PerformanceCalculationAdmin {
//设计绩效计算函数:通过循环计算所有员工的绩效,并打印信息
public void allPerformanceCalculation(PerformanceCalculation[] pe, Employee[] employees) {
for (int i = 0; i < pe.length; i++) {
System.out.println("姓名:" + employees[i].name);
System.out.println("性别:" + employees[i].sex);
System.out.println("工号:" + employees[i].number);
System.out.println("出生:" + employees[i].birthday);
System.out.println("职称:" + employees[i].professionalTitles);
pe[i].showWork();
System.out.println("该员工的绩效为:" + pe[i].doPerformanceCalculation() + "\n");
}
}
}
/**
* 增加实验员类别LaboratoryAssistant并实现绩效接口PerformanceCalculation
*/
class LaboratoryAssistant extends Employee implements PerformanceCalculation {
public double laboratoryLoad; //实验工作量
public double researchLoad; //科研工作量
//定义构造函数初始化
LaboratoryAssistant(String number, String name, int sex, String birthday, String professionalTitles, double laboratoryLoad, double researchLoad) {
this.number = number;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.professionalTitles = professionalTitles;
this.laboratoryLoad = laboratoryLoad;
this.researchLoad = researchLoad;
}
//定义实验教学方法
public void doLaboratory() {
System.out.println("实验员" + name + "正在实验教学,实验工作量为:" + laboratoryLoad);
}
//定义做科研的方法
public void doReSearch() {
System.out.println("实验员" + name + "正在做科研,科研工作量为:" + researchLoad);
}
//重写showWork,显示工作形式
public void showWork() {
doLaboratory();
doReSearch();
}
//计算各自的绩效
public double doPerformanceCalculation() {
return (laboratoryLoad - 240) / 240 + (researchLoad - 75) / 75;
}
}
/**
* 主测试类
*/
public class TestMainThree {
public static void main(String[] args) {
//创建管理类对象admin
PerformanceCalculationAdmin admin = new PerformanceCalculationAdmin();
//创建三类员工子类的对象,并分别对三个类别初始化
TeacherPeople teacherPeople = new TeacherPeople("001", "张三", 1, "2020年1月1日", "教授", 350, 150);
ResearchStaffPeople staffPeople = new ResearchStaffPeople("002", "李四", 1, "2020年2月3日", "院士", 900);
LaboratoryAssistant labAssistant = new LaboratoryAssistant("003", "赵五", 0, "2020年4月5日", "实验员", 450, 150);
//创建PerformanceCalculation对象数组和Employee对象数组
//同时赋值三个类别的对象
PerformanceCalculation[] pe = {teacherPeople, staffPeople, labAssistant};
Employee[] employees = {teacherPeople, staffPeople, labAssistant};
//调用allPerformanceCalculation计算绩效和显示员工信息
admin.allPerformanceCalculation(pe, employees);
}
}