4、 Cola公司的雇员分为以下若干类:(知识点:多态) [必做题]
• 4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
• 4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
• 4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
• 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
• 4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资
1 package text; 2 3 public class homework { 4 String name; 5 int month; 6 7 public ColaEmployee() { 8 } 9 10 public ColaEmployee(String name, int month) { 11 this.name = name; 12 this.month = month; 13 14 } 15 16 public double getSalary(int month) { 17 return 0; 18 } 19 } 20 21 22 package text; 23 24 public class SalariedEmployee extends ColaEmployee { 25 double monSalary; 26 27 public SalariedEmployee(String name, int month, double monSalary) { 28 super(name, month); 29 this.monSalary = monSalary; 30 } 31 32 public double getSalary(int month) { 33 if (super.month == month) { 34 return monSalary + 100; 35 } else { 36 return monSalary; 37 } 38 } 39 } 40 41 package text; 42 43 public class HourlyEmployee extends ColaEmployee { 44 private int hourSalary; 45 private int hourNum; 46 47 public HourlyEmployee(String name, int month, int hourSalary, int hourNum) { 48 super(name, month); 49 this.hourSalary = hourSalary; 50 this.hourNum = hourNum; 51 } 52 53 /* 54 * 情况: 1.每月工作超出160小时; 2.每月工作超出160 小时 + (该月过生日)100; 55 * 56 * 3.每月工作没超出160小时; 4.每月工作没超出160小时+ (该月过生日)100; 57 */ 58 public double getSalary(int month) { 59 if (super.month == month) { 60 if (hourNum > 160) { 61 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5 + 100; 62 } else { 63 return hourSalary * hourNum + 100; 64 } 65 } else { 66 if (hourNum > 160) { 67 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5; 68 } else { 69 return hourSalary * hourNum; 70 } 71 } 72 } 73 } 74 75 76 package text; 77 78 public class SalesEmployee extends ColaEmployee { 79 private int monthSales; 80 private double royaltyRate; 81 82 public SalesEmployee(String name, int month, int monthSales, double royaltyRate) { 83 super(name, month); 84 this.monthSales = monthSales; 85 this.royaltyRate = royaltyRate; 86 } 87 88 public double getSalary(int month) { 89 if (super.month == month) { 90 return monthSales * royaltyRate + 100; 91 } else { 92 return monthSales * royaltyRate; 93 } 94 } 95 } 96 97 package text; 98 99 public class Company { 100 public void getSalary(ColaEmployee c, int month) { 101 System.out.println(c.name + "在" + month + "月的月薪为:" + c.getSalary(month) + "元"); 102 } 103 } 104 105 106 package text; 107 108 public class TestCompany { 109 110 public static void main(String[] args) { 111 // TODO Auto-generated method stub 112 ColaEmployee[] cel = { new SalariedEmployee("张强", 7, 40000), 113 new HourlyEmployee("李红", 3, 200, 500), 114 new SalesEmployee("赵颖", 7, 4500000, 0.4) }; 115 for (int i = 0; i < cel.length; i++) { 116 new Company().getSalary(cel[i], 6); 117 } 118 } 119 120 }