标签:super 发放 void 方法 一个 string 数组 employee bsp
对象、继承、封装、多态、抽象类的组合应用
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该员工的工资增加100元。
(1)定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();toString()方法输出对象的name,number和birthday。
1 /** 2 * 定义一个Employee类,该类包含: 3 * private成员变量name,number,birthday,其中birthday 为MyDate类的对象; 4 * abstract方法earnings();toString()方法输出对象的name,number和birthday。 5 */ 6 public abstract class Employee { 7 8 private String name; 9 private int number; 10 private MyDate birthday; 11 12 public Employee(String name, int number, MyDate birthday) { 13 this.name = name; 14 this.number = number; 15 this.birthday = birthday; 16 } 17 18 abstract int earnings(); 19 20 @Override 21 public String toString() { 22 return "Employee [name=" + name + ", number=" + number + ", birthday=" 23 + birthday.toDateString() + "]"; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public int getNumber() { 35 return number; 36 } 37 38 public void setNumber(int number) { 39 this.number = number; 40 } 41 42 public MyDate getBirthday() { 43 return birthday; 44 } 45 46 public void setBirthday(MyDate birthday) { 47 this.birthday = birthday; 48 } 49 }
(2)MyDate类包含:
private成员变量month,day,year;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
1 /** 2 * MyDate类包含: 3 * private成员变量month,day,year; 4 * toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 5 */ 6 public class MyDate { 7 8 private int month; 9 private int day; 10 private int year; 11 12 public MyDate(int month, int day, int year) { 13 this.month = month; 14 this.day = day; 15 this.year = year; 16 } 17 18 public String toDateString(){ 19 return year + "年" + 20 month + "月" + 21 day + "日"; 22 } 23 24 public int getMonth() { 25 return month; 26 } 27 28 public void setMonth(int month) { 29 this.month = month; 30 } 31 32 public int getDay() { 33 return day; 34 } 35 36 public void setDay(int day) { 37 this.day = day; 38 } 39 40 public int getYear() { 41 return year; 42 } 43 44 public void setYear(int year) { 45 this.year = year; 46 } 47 }
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。该类包括:
private成员变量weeklySalary;
实现父类的抽象方法earnings(),该方法返回weeklySalary值;
toString()方法输出员工类型信息及员工的name,number,birthday。
1 /** 2 * 定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。该类包括: 3 * private成员变量weeklySalary; 4 * 实现父类的抽象方法earnings(),该方法返回weeklySalary值; 5 * toString()方法输出员工类型信息及员工的name,number,birthday。 6 */ 7 public class SalariedEmployee extends Employee { 8 9 private int weeklySalary; 10 11 public SalariedEmployee(String name, int number, MyDate birthday, 12 int weeklySalary) { 13 super(name, number, birthday); 14 this.weeklySalary = weeklySalary; 15 } 16 17 @Override 18 int earnings() { 19 return weeklySalary; 20 } 21 22 @Override 23 public String toString() { 24 return "EmployeeType: SalariedEmployee, " + super.toString(); 25 } 26 27 public int getWeeklySalary() { 28 return weeklySalary; 29 } 30 31 public void setWeeklySalary(int weeklySalary) { 32 this.weeklySalary = weeklySalary; 33 } 34 }
(4)参照SalariedEmployee类定义HourlyEmployee类,
实现按小时计算工资的员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;toString()方法输出员工类型信息及员工的name,number,birthday。
1 /** 2 * 参照SalariedEmployee类定义HourlyEmployee类, 3 * 实现按小时计算工资的员工处理。该类包括: 4 * private成员变量wage和hour; 5 * 实现父类的抽象方法earnings(),该方法返回wage*hour值;toString()方法输出员工类型信息及员工的name,number,birthday。 6 */ 7 public class HourlyEmployee extends Employee { 8 9 public HourlyEmployee(String name, int number, MyDate birthday, int wage, 10 int hour) { 11 super(name, number, birthday); 12 this.wage = wage; 13 this.hour = hour; 14 } 15 16 private int wage; 17 private int hour; 18 19 @Override 20 int earnings() { 21 return wage * hour; 22 } 23 24 @Override 25 public String toString() { 26 return "EmployeeType: HourlyEmployee, "+ super.toString(); 27 } 28 29 public int getWage() { 30 return wage; 31 } 32 33 public void setWage(int wage) { 34 this.wage = wage; 35 } 36 37 public int getHour() { 38 return hour; 39 } 40 41 public void setHour(int hour) { 42 this.hour = hour; 43 } 44 }
(5)定义PayrollSystem类,创建Employee变量数组,该数组存放各类雇员对象的引用。
利用循环结构,输入本月月份值,输出各个对象的类型,name,number,birthday,以及该对象生日。
如果本月是某个Employee对象的生日,还要输出增加工资信息。
1 /** 2 * 定义PayrollSystem类,创建Employee变量数组,该数组存放各类雇员对象的引用。 3 * 利用循环结构,输入本月月份值,输出各个对象的类型,name,number,birthday,以及该对象生日。 4 * 如果本月是某个Employee对象的生日,还要输出增加工资信息。 5 */ 6 public class PayrollSystem { 8 public static void main(String[] args) { 10 Employee [] emps = new Employee[5]; 11 12 emps[0] = new SalariedEmployee("Tom", 1001, new MyDate(4, 2, 1990), 200); 13 emps[1] = new SalariedEmployee("Jerry", 1002, new MyDate(5, 2, 1989), 300); 14 15 emps[2] = new HourlyEmployee("Mike", 1003, new MyDate(6, 4, 1987), 100, 4); 16 emps[3] = new HourlyEmployee("Rose", 1004, new MyDate(4, 10, 1987), 200, 5); 17 emps[4] = new HourlyEmployee("Bob", 1005, new MyDate(8, 4, 1987), 300, 4); 18 19 for(Employee emp: emps){ 20 System.out.println(emp); 21 22 if(emp.getBirthday().getMonth() == 4){ 23 System.out.println("生日: 加工资 100 元"); 24 } 25 System.out.println(); 26 } 27 } 28 }
标签:super 发放 void 方法 一个 string 数组 employee bsp
原文地址:http://www.cnblogs.com/justdoitba/p/7229234.html