某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的生日月份。方法:getSalary(int month) 根
据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
SalariedEmployee:Employee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分
按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数SalesEmployee:Employee
的子类, 销售人员, 工资由月销售额和提成率决定。属性: 月销售额、提成率
BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上
销售提成部分。属性:底薪。
写一个程序,把若干各种类型的员工放在一个Employee 数组里,写一个函数,打印出某月
每个员工的工资数额。注意:不允许非私有化属性。
BasePlusSaleEmployee.java
package ex4_2; public class BasePlusSaleEmployee extends SalesEmployee { private double baseSalary; private double addRate; private double sale; public BasePlusSaleEmployee(double sale,double addRate,double baseSalary) { super(sale, addRate); // TODO Auto-generated constructor stub setAddRate(addRate); setBaseSalary(baseSalary); setSale(sale); } @Override public double getSalary() { SalesEmployee a=new SalesEmployee(sale, addRate); // TODO Auto-generated method stub return baseSalary+a.getSalary(); } public double getBaseSalary() { return baseSalary; } public void setBaseSalary(double baseSalary) { this.baseSalary = baseSalary; } public double getAddRate() { return addRate; } public void setAddRate(double addRate) { this.addRate = addRate; } public double getSale() { return sale; } public void setSale(double sale) { this.sale = sale; } }
Employee.java
1 package ex4_2; 2 3 public class Employee { 4 5 private int monthN=6;//假设现在六月 6 private int month; 7 private int salary=100; 8 9 public Employee() { 10 setMonth(month); 11 } 12 13 public int getMonth() { 14 return month; 15 } 16 17 public void setMonth(int month) { 18 this.month = month; 19 } 20 public double getSalary() { 21 if(monthN==month) { 22 return salary; 23 } 24 else return 0; 25 } 26 }
HourlyEmployee.java
package ex4_2; public class HourlyEmployee extends Employee { private double hourlySalary; private int hour; public HourlyEmployee(double hourlySalary,int hour) { super(); setHour(hour); setHourlySalary(hourlySalary); } @Override public double getSalary() { if(hour>160) return 160*hourlySalary+(hour-160)*1.5*hourlySalary; else return hour*hourlySalary; } public double getHourlySalary() { return hourlySalary; } public void setHourlySalary(double hourlySalary) { this.hourlySalary = hourlySalary; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } }
SalariedEmployee.java
package ex4_2; public class SalariedEmployee extends Employee { private double salary; public SalariedEmployee(double salary) { // TODO Auto-generated constructor stub super(); setSalary(salary); } @Override public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
SalesEmployee.java
1 package ex4_2; 2 3 public class SalesEmployee extends Employee { 4 5 private double sale; 6 private double addRate; 7 8 public SalesEmployee(double sale,double addRate) { 9 setAddRate(addRate); 10 setSale(sale); 11 } 12 13 14 @Override 15 public double getSalary() { 16 // TODO Auto-generated method stub 17 return sale*addRate; 18 } 19 20 21 public double getSale() { 22 return sale; 23 } 24 25 public void setSale(double sale) { 26 this.sale = sale; 27 } 28 29 public double getAddRate() { 30 return addRate; 31 } 32 33 public void setAddRate(double addRate) { 34 this.addRate = addRate; 35 } 36 37 38 }
Tester.java
package ex4_2; import java.util.Calendar; //import java.util.Scanner; public class Tester { public static void main (String[] args) { Employee employee[]= { new SalariedEmployee(1000), new HourlyEmployee(16,150), new SalesEmployee(500,0.2), new BasePlusSaleEmployee(500,0.2,1000) }; Calendar cal=Calendar.getInstance(); cal.set(1994, 6, 18); for(int i=0;i<employee.length;i++) { int month=cal.get(Calendar.MONTH); Employee a=new Employee(); a.setMonth(month); double welfare=a.getSalary(); double salary=employee[i].getSalary(); System.out.println("第"+(i+1)+"个:"+(welfare+salary)); } } }