练习一
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。 实验说明: (1)定义一个Employee类,该类包含: private成员变量name,number,birthday,其中birthday 为MyDate类的对象; 提供必要的构造器; abstract方法earnings(),返回工资数额; toString()方法输出对象的name,number和birthday。 (2)MyDate类包含: private成员变量year,month,day; 提供必要的构造器; toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。 该类包括:private成员变量monthlySalary; 提供必要的构造器; 实现父类的抽象方法earnings(),该方法返回monthlySalary值; toString()方法输出员工类型信息及员工的name,number,birthday。比如:SalariedEmployee[name = '',number = ,birthday=xxxx年xx月xx日] (4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括: private成员变量wage和hour; 提供必要的构造器; 实现父类的抽象方法earnings(),该方法返回wage*hour值; toString()方法输出员工类型信息及员工的name,number,birthday。 (5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。 利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。 当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。 //提示: //定义People类型的数组People c1[]=new People[10]; //数组元素赋值 c1[0]=new People("John","0001",20); c1[1]=new People("Bob","0002",19); //若People有两个子类Student和Officer,则数组元素赋值时,可以使父类类型的数组元素指向子类。 c1[0]=new Student("John","0001",20,85.0); c1[1]=new Officer("Bob","0002",19,90.5);
package chapter08_oop3.src.com.atguigu07._abstract.exer2; /** * ClassName: Employee * Package: chapter08_oop3.src.com.atguigu07._abstract.exer2 * Description: * * (1)定义一个Employee类,该类包含: * * private成员变量name,number,birthday,其中birthday 为MyDate类的对象; * 提供必要的构造器; * abstract方法earnings(),返回工资数额; * toString()方法输出对象的name,number和birthday。 * * @Author 小白 * @Create 2024/4/4 12:48 * @Version 1.0 */ public abstract class Employee { private String name; private int number; private MyDate birthday; public Employee() { } public Employee(String name, int number, MyDate birthday) { this.name = name; this.number = number; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public MyDate getBirthday() { return birthday; } public void setBirthday(MyDate birthday) { this.birthday = birthday; } public abstract double earnings(); public String toString(){ return "name = " + name + ",number = " + number + ", birthday = " + birthday.toDateString(); } }
package chapter08_oop3.src.com.atguigu07._abstract.exer2; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate; /** * ClassName: HourlyEmployee * Description: * 参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括: * private成员变量wage和hour; * 提供必要的构造器; * 实现父类的抽象方法earnings(),该方法返回wage*hour值; * toString()方法输出员工类型信息及员工的name,number,birthday。 * @Author 尚硅谷-宋红康 * @Create 15:45 * @Version 1.0 */ public class HourlyEmployee extends Employee { private double wage;//单位小时的工资 private int hour;//月工作的小时数 public HourlyEmployee() { } public HourlyEmployee(String name, int number, MyDate birthday, double wage, int hour) { super(name, number, birthday); this.wage = wage; this.hour = hour; } public double getWage() { return wage; } public void setWage(double wage) { this.wage = wage; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } @Override public double earnings() { return wage * hour; } public String toString(){ return "HourlyEmployee[" + super.toString() + "]"; } }
package chapter08_oop3.src.com.atguigu07._abstract.exer2; /** * ClassName: MyDate * Package: chapter08_oop3.src.com.atguigu07._abstract.exer2 * Description: * * (2)MyDate类包含: * private成员变量year,month,day; * 提供必要的构造器; * toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 * * @Author 小白 * @Create 2024/4/4 12:49 * @Version 1.0 */ public class MyDate { private int year; private int month; private int day; public MyDate() { } public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String toDateString() { return year +"年" +month +"月" + day + "日"; } }
package chapter08_oop3.src.com.atguigu07._abstract.exer2; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.HourlyEmployee; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate; import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.SalariedEmployee; import java.util.Scanner; /** * ClassName: PayrollSystem * Description: * 定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。 * 利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday。 * 当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。 * * @Author 尚硅谷-宋红康 * @Create 15:47 * @Version 1.0 */ public class PayrollSystem { public static void main(String[] args) { Scanner scan = new Scanner(System.in); chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee[] emps = new Employee[2]; emps[0] = new SalariedEmployee("张小亮",1001, new chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate(1992,12,21),18000); emps[1] = new HourlyEmployee("侯少鹏",1002,new MyDate(1997,11,12), 240,100); System.out.println("请输入当前的月份:"); int month = scan.nextInt(); for (int i = 0; i < emps.length; i++) { System.out.println(emps[i].toString()); System.out.println("工资为:" + emps[i].earnings()); if(month == emps[i].getBirthday().getMonth()){ System.out.println("生日快乐!加薪100"); } } scan.close(); } }
package chapter08_oop3.src.com.atguigu07._abstract.exer2; /** * ClassName: SalariedEmployee * Package: chapter08_oop3.src.com.atguigu07._abstract.exer2 * Description: * * (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。 * 该类包括:private成员变量monthlySalary; * 提供必要的构造器; * 实现父类的抽象方法earnings(),该方法返回monthlySalary值; * toString()方法输出员工类型信息及员工的name,number,birthday。比如:SalariedEmployee[name = '',number = ,birthday=xxxx年xx月xx日] * * @Author 小白 * @Create 2024/4/4 13:19 * @Version 1.0 */ public class SalariedEmployee extends Employee{ private double monthlySalary; //月工资 public SalariedEmployee() { } @Override public double earnings() { return monthlySalary; } public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) { super(name, number, birthday); this.monthlySalary = monthlySalary; } // public double getMonthlySalary() { // return monthlySalary; // } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } public String toString(){ return "SalariedEmployee[" + super.toString() + "]"; } }