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 yjm; 2 3 public class ColaEmployee { 4 String name; 5 int month; 6 7 public ColaEmployee() { 8 9 super(); 10 } 11 12 public ColaEmployee(String name, int month) { 13 super(); 14 this.name = name; 15 this.month = month; 16 } 17 18 public double getSalary(int month) { 19 return 0; 20 } 21 }
1 package yjm; 2 3 public class SalariedEmployee extends ColaEmployee{ 4 double yuexin; 5 6 public SalariedEmployee(String name,int month,double yuexin) { 7 super(name,month); 8 this.yuexin=yuexin; 9 } 10 11 public double getSalary(int month) { 12 if (super.month==month) { 13 return yuexin +100; 14 }else { 15 return yuexin; 16 } 17 18 } 19 20 }
package yjm; public class HourlyEmployee extends ColaEmployee{ private int hourSalary; private int num; public HourlyEmployee(String name,int month,int hourSalary, int num) { super(name,month); this.hourSalary = hourSalary; this.num = num; } public double getSalary(int month) { if (super.month == month) { if (num>160) { return hourSalary * 160 + hourSalary * (num - 160) * 1.5 +100; } else { return hourSalary * num + 100; } } else { if (num >160) { return hourSalary * 160 + hourSalary * (num - 160) * 1.5; } else { return hourSalary * num; } } } }
1 package yjm; 2 3 public class SalesEmployee extends ColaEmployee { 4 private int monthSales; 5 private double royaltyRate; 6 public SalesEmployee(String name, int month, int monthSales, double royaltyRate) { 7 super(name, month); 8 this.monthSales = monthSales; 9 this.royaltyRate = royaltyRate; 10 } 11 public double getSalary(int month) { 12 if (super.month == month) 13 { 14 return monthSales * royaltyRate + 100; 15 } else { 16 return monthSales * royaltyRate; 17 } 18 } 19 20 }
public class Company { public void getSalary(ColaEmployee c,int month) { System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month)+"元"); } }
1 package yjm; 2 3 public class TestCompany { 4 public static void main(String[] args) { 5 6 ColaEmployee[] all = { 7 new SalariedEmployee("asd", 5, 30000), 8 new HourlyEmployee("qwe", 4, 100, 300), 9 new SalesEmployee("zxc", 3, 7000000, 0.3) 10 }; 11 12 for (int i = 0; i < all.length; i++) { 13 new Company().getSalary(all[i],5); 14 } 15 } 16 }