首页 > 代码库 > 使用Java继承的特性实现员工工资管理

使用Java继承的特性实现员工工资管理

父类:

 1 /** 2  * 员工总类 3  * @author Administrator 4  * 5  */ 6 public class Employee { 7  8     private String name;//员工名字 9     private int birthMonth;//员工的生日月份10     public String getName() {11         return name;12     }13     public void setName(String name) {14         this.name = name;15     }16     public int getBirthMonth() {17         return birthMonth;18     }19     public void setBirthMonth(int birthMonth) {20         this.birthMonth = birthMonth;21     }22     23     /*24      * 根据参数月份来确定工资,如果该月员工过生日25      */26     public double getSalary(int month){27         return 0;28     }29     30     31 }

拿固定工资的员工类:

 

 1 /** 2  * 拿固定工资的员工 3  * @author Administrator 4  * 5  */ 6 public class SalariedEmployee extends Employee { 7  8     private double salary;//月薪 9 10     public double getSalary() {11         return salary;12     }13 14     public void setSalary(double salary) {15         this.salary = salary;16     }17 18     @Override19     public double getSalary(int month) {20         double temp = 0 ;21         if(this.getBirthMonth() == month){22             temp += 100;23         }24         25         return temp+salary;26     }27     28     29     30 }

 

 按小时拿工资的员工类

 1 /** 2  * 按小时拿工资的员工 3  * @author Administrator 4  * 5  */ 6 public class HourlyEmployee extends Employee { 7  8     private double hourSalary;//每小时的工资 9     private double hours;//每月工作的小时数10     public double getHourSalary() {11         return hourSalary;12     }13     public void setHourSalary(double hourSalary) {14         this.hourSalary = hourSalary;15     }16     public double getHours() {17         return hours;18     }19     public void setHours(double hours) {20         this.hours = hours;21     }22     23     public double hSalary(){24         if(hours <= 160){25             return hours*hourSalary;26         }else{27             return 160*hourSalary+(hours-160)*hourSalary*1.5;28         }29     }30     31     @Override32     public double getSalary(int month) {33         double temp = 0 ;34         if(this.getBirthMonth() == month){35             temp += 100;36         }37         38         return temp+this.hSalary();39     }40     41 }

 

使用Java继承的特性实现员工工资管理