1. 程式人生 > >對象、繼承、封裝、多態、抽象類的組合應用

對象、繼承、封裝、多態、抽象類的組合應用

extends trac out ring -h system類 ber 小時 信息

對象、繼承、封裝、多態、抽象類的組合應用

編寫工資系統,實現不同類型員工(多態)的按月發放工資。如果當月出現某個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 }

對象、繼承、封裝、多態、抽象類的組合應用