1、员工类Employee:
public abstract class Employee {
//姓名
private String name;
//工号
private String num;
//生日
private MyDate birthday;
//构造方法
public Employee(String name,String num,MyDate birthday){
this.name = name;
this.num = num;
this.birthday = birthday;
}
//无参构造 不写也行
public Employee() {
}
//抽象类方法
public abstract double earnings();
@Override
public String toString() {
return "name:"+name+",num:"+num+",birthday:"+birthday;
}
public MyDate getBirthday() {
return birthday;
}
public String getName() {
return name;
}
}
2、小时工HourlyEmployee:
public class HourlyEmployee extends Employee{
//小时工 按小时结算工资
//每小时的工资
private double wage;
//时间
private double hour;
public HourlyEmployee(String name,String num,MyDate birthday,double wage,double hour){
//super继承父类
super(name, num, birthday);
this.wage = wage;
this.hour = hour;
}
//重写父类Employee的earnings()方法
@Override
public double earnings() {
return wage * hour;
}
@Override
public String toString() {
return super.toString()+this.earnings();
}
}
3、正式员工SalariedEmployee:
public class SalariedEmployee extends Employee{
//正式员工 按每月发工资
private double monthlySalary;
public SalariedEmployee(String name,String num,MyDate birthday,double monthlySalary){
super(name, num, birthday);
this.monthlySalary = monthlySalary;
}
public SalariedEmployee(){
}
//重写父类Employee的earnings()方法
@Override
public double earnings() {
return monthlySalary;
}
@Override
public String toString() {
return super.toString() + ",薪水:"+this.earnings();
}
}
4、日期类MyDate:
public class MyDate {
private int year;
private int month;
private int day;
//对年月日进行赋值
public MyDate(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return year + "年" + month + "月" + day + "日";
}
//因为需要获取员工是否生日
//所以需要调用getMonth()方法
public int getMonth() {
return month;
}
}
5、财务系统类PayrollSystem:
import java.util.Arrays;
public class PayrollSystem {
//创建数组 用于存储员工信息
private Employee[] employees;
public PayrollSystem(){
}
@Override
public String toString() {
return Arrays.toString(employees);
}
//有参构造
public PayrollSystem(Employee[] employees){
this.employees = employees;
}
//获取数组
public Employee[] getEmployees() {
return employees;
}
//更改数组
public void setEmployees(Employee[] employees) {
this.employees = employees;
}
}
6、测试类EmployeeTest:
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args) {
//创建一个PayrollSystem好比创建一个财务系统
//用于存储员工信息
PayrollSystem payrollSystem = new PayrollSystem();
MyDate e1B = new MyDate(2000,10,1);
Employee e1 = new SalariedEmployee("张三","A001",e1B,5000.0);
Employee e2 = new SalariedEmployee("李四","A002",e1B,5000.0);
//创建数组 给它赋值 将他赋给payroSystem的数组赋值
Employee[] employees = {e1,e2};
//将他赋给payrollSystem的数组
payrollSystem.setEmployees(employees);
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("功能列表:" +
"1、列出员工信息" +
"2、输入月份" +
"3、退出");
System.out.print("请输入要执行的命令:");
int s1 = scanner.nextInt();
if(s1 == 1){
for (int i = 0;i
写的比较匆忙,后期再完善一下



