第十一周作业
4、Cola公司的雇员分为以下若干类:(知识点:多态)
(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。
属性:月薪
(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
属性:每小时的工资、每月工作的小时数
(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
1 package Cola; 2 3 public class ColaEmployee { 4 String name; 5 int month; 6 7 public ColaEmployee() { 8 super(); 9 10 } 11 12 13 public ColaEmployee(String name, int month) { 14 super(); 15 this.name = name; 16 this.month = month; 17 } 18 19 20 public double getSalary(int month){ 21 return month; 22 } 23 24 }
1 package Cola; 2 3 public class SalariedEmployee extends ColaEmployee { 4 double monthsalary ; 5 6 public SalariedEmployee() { 7 super(); 8 9 } 10 11 public SalariedEmployee(String name, int mouth ,double monthsalary) { 12 super(name, mouth); 13 this.monthsalary = monthsalary; 14 15 } 16 @Override 17 public double getSalary(int month) { 18 19 if (super.month == month) { 20 return monthsalary + 100; 21 } else { 22 return monthsalary; 23 } 24 25 } 26 27 28 }
1 package Cola; 2 3 public class HourlyEmployee extends ColaEmployee { 4 int hourSalary; 5 int hour; 6 7 public HourlyEmployee() { 8 super(); 9 } 10 11 public HourlyEmployee(String name, int month, int hourSalary, int hour) { 12 super(name, month); 13 this.hourSalary = hourSalary; 14 this.hour = hour; 15 } 16 @Override 17 18 public double getSalary(int month) { 19 if(super.month == month){ 20 if (hour > 160) { 21 return hourSalary * 160+ (hour-160)*1.5*hourSalary + 100; 22 } else { 23 return hourSalary * hour + 100; 24 } 25 }else{ 26 if (hour > 160) { 27 return hourSalary * 160 +(hour - 160)*1.5*hourSalary ; 28 } else { 29 return hourSalary * hour; 30 } 31 32 } 33 34 35 36 37 } 38 39 40 }
1 package Cola; 2 3 public class SalesEmployee extends ColaEmployee { 4 double monthsalary; 5 double monthcommission ; 6 public SalesEmployee() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public SalesEmployee(String name,int month,double monthsalary, double monthcommission) { 11 super(name,month); 12 this.monthsalary = monthsalary; 13 this.monthcommission = monthcommission; 14 } 15 @Override 16 public double getSalary(int month) { 17 if (super.month == month) { 18 return monthsalary*monthcommission + 100; 19 } else { 20 return monthsalary*monthcommission; 21 } 22 23 } 24 25 26 27 28 }
1 package Cola; 2 3 public class Company { 4 public void getSalary(ColaEmployee c, int month) { 5 System.out.println(c.name + "在" + month + "月的月薪是:" + c.getSalary(month) + "元"); 6 } 7 8 }
1 package Cola; 2 3 public class TestCompany { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 ColaEmployee[] cel = { 8 new SalariedEmployee("拿固定工资的员工", 1, 8500), 9 new HourlyEmployee("按小时拿工资的员工", 2, 100,85), 10 new SalesEmployee("销售人员", 3, 70000, 0.1) 11 }; 12 for (int i = 0; i < cel.length; i++) { 13 new Company().getSalary(cel[i],3); 14 } 15 16 17 } 18 19 }