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 work15;
 2 
 3 public class ColaEmployee {
 4     protected String name;
 5     protected int birthmonth;
 6     
 7     
 8     public ColaEmployee() {
 9         super();
10     }
11     public ColaEmployee(String name, int month) {
12         super();
13         this.name = name;
14         this.birthmonth = birthmonth;
15     }
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public int getmonth() {
23         return birthmonth;
24     }
25     public void setmonth(int birthmonth) {
26         this.birthmonth = birthmonth;
27     }
28     public double getSalary(int month){
29         return 0;
30     }
31 
32 }
 1 package work15;
 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 birthmonth, double monthSalary) {
12         super(name, birthmonth);
13         this.monthSalary= monthSalary;
14         
15     }
16     public double getSalary(int month){
17         if(super.birthmonth==month){
18             return monthSalary +100;
19         }else{
20             return monthSalary;
21         }
22     }
23     
24 
25 }
 1 package work15;
 2 
 3 public class HourlyEmployee extends ColaEmployee{
 4     double hourSalary;
 5     int hourSum;
 6     public HourlyEmployee() {
 7         super();
 8         
 9     }
10     public HourlyEmployee(String name, int birthmonth, double hourSalary,int hourSum) {
11         super(name, birthmonth);
12         this.hourSalary= hourSalary;
13         this.hourSum= hourSum;
14         
15     }
16     public double getSalary(int month){
17         if (super.birthmonth==month) {
18             if (hourSum>160) {
19                 return  hourSalary*160+hourSalary*(hourSum-160)*1.5+100;
20             }else{
21                 return  hourSalary*160+hourSalary*(hourSum-160)*1.5;
22             }
23         }else{
24             if (hourSum>160) {
25                 return  hourSalary*hourSum+100;
26             }else{
27                 return  hourSalary*hourSum;
28             }
29         }
30     }
31     
32 
33 }
 1 package work15;
 2 
 3 public class SalesEmployee extends ColaEmployee{
 4     int monthSales;
 5     double royaltyRate;
 6     public SalesEmployee() {
 7         super();
 8         
 9     }
10     public SalesEmployee(String name, int birthmonth, int monthSales,double royaltyRate) {
11         super(name, birthmonth);
12         this.monthSales=monthSales ;
13         this.royaltyRate=royaltyRate ;
14         
15     }
16     public double getSalary(int month){
17         if (super.birthmonth==month) {
18             return monthSales * royaltyRate +100;
19         }else{
20             return monthSales * royaltyRate ;
21         }
22     }
23     
24 
25 }
1 package work15;
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 work15;
 2 
 3 public class TestCompany {
 4 
 5 
 6     public static void main(String[] args) {
 7         ColaEmployee[] cel={
 8                 new SalariedEmployee("月薪员工",7,3000),
 9                 new HourlyEmployee("时薪员工", 12, 10, 200),
10                 new SalesEmployee("销售提成员工", 3, 300, 10)
11         };
12         for (int i = 0; i < cel.length; i++) {
13             new Company().getSalary(cel[i], 3);
14         }
15 
16     }
17 
18 }

 

• 5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
• class apple
• {
• public apple()
• {
• System.out.println(―创建了一个苹果类的对象‖);
}
• }
课后作业
• 类图如下:
• 5.3 要求从控制台输入一个字符串,根据字符串的
值来判断创建三种水果中哪个类的对象

 1 package work;
 2 
 3 import java.util.Scanner;
 4 
 5 public interface Fruit {
 6 
 7 }
 8 class Apple implements Fruit {
 9      public Apple() {
10         System.out.println("创建了一个苹果对象");
11     }
12  }
13  
14  class Banana implements Fruit {
15      public Banana() {
16         System.out.println("创建了一个香蕉对象");
17      }
18  }
19  
20  class Putao implements Fruit {
21      public Putao() {
22          System.out.println("创建了一个葡萄对象");
23      }
24  }
25 
26  class Gardener {
27      public Fruit create() {
28          Fruit f = null;
29          Scanner input = new Scanner(System.in);
30         String name = input.next();
31          if (name.equals("苹果")) {
32              f = new Apple();
33          } else if (name.equals("香蕉")) {
34              f = new Banana();
35          } else if (name.equals("葡萄")) {
36              f = new Putao();
37          } else {
38             System.out.println("不会种");
39          }
40          return f;
41  
42      }
43  }
1 package work;
2 
3 public class Test {
4 
5      public static void main(String[] args) {
6              Gardener g = new Gardener();
7                  g.create();
8       }
9 }

posted on 2021-06-18 19:44  李育博  阅读(35)  评论(0编辑  收藏  举报