5.15java作业

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 pro1;
 2 
 3 public class ColaEmployee {
 4     String name;
 5     int month;
 6 
 7     public ColaEmployee() {
 8 
 9     }
10 
11     public ColaEmployee(String name, int month) {
12         super();
13         this.name = name;
14         this.month = month;
15     }
16 
17     public double getSalary(int month) {
18         return 0;
19     }
20 
21 }
 1 package pro2;
 2 
 3 public class SalariedEmployee extends ColaEmployee {
 4     double monSalary;
 5 
 6     public SalariedEmployee() {
 7         super();
 8     }
 9 
10     public SalariedEmployee(String name, int month, double monSalary) {
11         super(name, month);
12         this.monSalary = monSalary;
13     }
14 
15     public double getSalary(int month) {
16         if (super.month == month) {
17             return monSalary + 100;
18         } else {
19             return monSalary;
20         }
21     }
22 
23 }
 1 package pro3;
 2 
 3 public class HourlyEmployee extends ColaEmployee {
 4     private int hourSalary;
 5     private int hourNum;
 6 
 7     public HourlyEmployee(String name, int month, int hourSalary, int hourNum) {
 8         super(name, month);
 9         this.hourSalary = hourSalary;
10         this.hourNum = hourNum;
11 
12     }
13 
14     public double getSalary(int month) {
15         if (super.month == month) {
16             if (hourNum > 160) {
17                 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5 + 100;
18             } else {
19                 return hourSalary * hourNum + 100;
20             }
21         } else {
22             if (hourNum > 160) {
23                 return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5;
24             } else {
25                 return hourSalary * hourNum;
26             }
27         }
28 
29     }
30 
31 }
 1 package pro4;
 2 
 3 public class SalesEmployee extends ColaEmployee {
 4     private int monthSales;
 5     private double royaltyRate;
 6 
 7     public SalesEmployee(String name, int month, int monthSales, double royaltyRate) {
 8         super(name, month);
 9         this.monthSales = monthSales;
10         this.royaltyRate = royaltyRate;
11     }
12 
13     public double getSalary(int month) {
14         if (super.month == month) {
15             return monthSales * royaltyRate + 100;
16         } else {
17             return monthSales * royaltyRate;
18         }
19     }
20 
21 }
1 package pro5;
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 pro6;
 2 
 3 public class Text {
 4     public static void main(String[] args) {
 5         ColaEmployee[] cel = { new         
 6     SalariedEmployee("salariedEmployee", 6, 25000), 
 7                 new HourlyEmployee("hourlyEmployee", 5, 200, 200), 
 8                 new SalesEmployee("salesEmployee", 3, 60000, 0.3)
 9         };
10         for (int i = 0; i < cel.length; i++) {
11             new Company().getSalary(cel[i], 7);
12         }
13     }
14 
15 }

 

posted @ 2020-05-15 23:32  潘宇航  阅读(167)  评论(0编辑  收藏  举报