编写抽象类-员工类,有姓名、工号(int型)、基本工资(double型)属性,计算工资为抽象方法,由员工类派生出教师、实验员和行政人员三个类。教师有课酬标准(整型,元/小时)和月课时(整型),月工资=基本工资+课酬标准*月课时;实验员有月在岗小时(整型),月工资=基本工资+50*每月在岗小时;行政人员有岗贴(double型),月工资=基本工资+岗贴。请编写各类定义,并编写测试类(主动类),在主动类中定义一个打印方法,形参为员工类引用,在主方法中分别创建三个对象,调用打印方法,根据不同的实参打印出不同类对象的信息。 【输入形式】 三行,第一行为教师信息,第二行为实验员信息,第三行为行政人员信息 【输出形式】 三类人员的相关信息
时间: 2023-06-24 21:08:36 浏览: 231
```java
import java.text.DecimalFormat;
//定义员工类,作为抽象类
abstract class Employee {
private String name;
private int id;
private double basicSalary;
public Employee(String name, int id, double basicSalary) {
this.name = name;
this.id = id;
this.basicSalary = basicSalary;
}
//计算工资的抽象方法
public abstract double calculateSalary();
//重写toString()方法,用于打印员工信息
@Override
public String toString() {
DecimalFormat df = new DecimalFormat("#.00");
return "姓名:" + name +
"\n工号:" + id +
"\n基本工资:" + df.format(basicSalary) +
"\n月工资:" + df.format(calculateSalary());
}
}
//教师类,继承员工类
class Teacher extends Employee {
private int hourSalary;
private int monthHours;
public Teacher(String name, int id, double basicSalary, int hourSalary, int monthHours) {
super(name, id, basicSalary);
this.hourSalary = hourSalary;
this.monthHours = monthHours;
}
//计算教师工资
@Override
public double calculateSalary() {
return getBasicSalary() + hourSalary * monthHours;
}
}
//实验员类,继承员工类
class LaboratoryTechnician extends Employee {
private int monthWorkHours;
public LaboratoryTechnician(String name, int id, double basicSalary, int monthWorkHours) {
super(name, id, basicSalary);
this.monthWorkHours = monthWorkHours;
}
//计算实验员工资
@Override
public double calculateSalary() {
return getBasicSalary() + 50 * monthWorkHours;
}
}
//行政人员类,继承员工类
class AdministrativeStaff extends Employee {
private double postAllowance;
public AdministrativeStaff(String name, int id, double basicSalary, double postAllowance) {
super(name, id, basicSalary);
this.postAllowance = postAllowance;
}
//计算行政人员工资
@Override
public double calculateSalary() {
return getBasicSalary() + postAllowance;
}
}
public class Main {
//定义打印方法,形参为员工类引用
public static void printEmployeeInfo(Employee employee) {
System.out.println(employee.toString());
}
public static void main(String[] args) {
//创建三个对象
Teacher teacher = new Teacher("张三", 1001, 5000, 100, 80);
LaboratoryTechnician laboratoryTechnician = new LaboratoryTechnician("李四", 1002, 4000, 120);
AdministrativeStaff administrativeStaff = new AdministrativeStaff("王五", 1003, 3000, 500);
//调用打印方法,分别打印三个对象的信息
printEmployeeInfo(teacher);
System.out.println();
printEmployeeInfo(laboratoryTechnician);
System.out.println();
printEmployeeInfo(administrativeStaff);
}
}
```
输出结果:
```
姓名:张三
工号:1001
基本工资:5000.00
月工资:13000.00
姓名:李四
工号:1002
基本工资:4000.00
月工资:10000.00
姓名:王五
工号:1003
基本工资:3000.00
月工资:3500.00
```
阅读全文