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 数组里,并单元出数组中每个员工当月的工资。
private String name; private int birmonth; public void getSalary(int month) { System.out.println("month:"+month); }
private double monthsalary; private int month; public void salarySalariedEmployee(String name, int workmonth, int birmonth, double monthsalary) { if (workmonth == birmonth) { System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + (monthsalary * workmonth + 100)); } else { System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + monthsalary * workmonth); } }
private double hoursalary; private int monthhour; public void salaryHourlyEmployee(String name, int birmonth, int workmonth, double hoursalary, int monthhour) { if (monthhour > 160) { if (workmonth == birmonth) { double sumsalary = (monthhour * workmonth - 160) * hoursalary * 1.5 + 160 * hoursalary + 100; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } else { double sumsalary = (monthhour * workmonth - 160) * hoursalary * 1.5 + 160 * hoursalary; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } } else { if (workmonth == birmonth) { double sumsalary = monthhour * workmonth * hoursalary + 100; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } else { double sumsalary = monthhour * workmonth * hoursalary; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } } }
private double monthsale; private double rate; public void salarySalesEmployee(String name, int birmonth, int workmonth, double monthsale, double rate) { if (workmonth == birmonth) { double sumsalary = (monthsale * workmonth) * rate + 100; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } else { double sumsalary = (monthsale * workmonth) * rate; System.out.println("name:" + name + ",birmonth:" + birmonth + ",sumSalary:" + sumsalary); } }
public static void show(ColaEmployee ce) { if (ce instanceof SalariedEmployee) { SalariedEmployee se = (SalariedEmployee) ce; se.salarySalariedEmployee("aa", 7, 6, 6700); } else if (ce instanceof HourlyEmployee) { HourlyEmployee he = (HourlyEmployee) ce; he.salaryHourlyEmployee("bb", 6, 6, 15, 180); } else if (ce instanceof SalesEmployee) { SalesEmployee see = (SalesEmployee) ce; see.salarySalesEmployee("cc", 5, 5, 5200, 0.5); } }
package homework; public class TestCompany { public static void main(String[] args) { // TODO Auto-generated method stub Company C = new Company(); SalariedEmployee se = new SalariedEmployee(); C.show(se); System.out.println("**************************************"); HourlyEmployee he = new HourlyEmployee(); C.show(he); System.out.println("**************************************"); SalesEmployee see = new SalesEmployee(); C.show(see); } }