java第15周作业

1.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 public class Colaemployee {
 2  
 3     String name;
 4     int month;
 5      
 6     public Colaemployee() {
 7         super();
 8         // TODO Auto-generated constructor stub
 9     }
10  
11     public Colaemployee(String name, int month) {
12         super();
13         this.name = name;
14         this.month = month;
15     }
16  
17     public String getName() {
18         return name;
19     }
20  
21     public void setName(String name) {
22         this.name = name;
23     }
24  
25     public int getMonth() {
26         return month;
27     }
28  
29     public void setMonth(int month) {
30         this.month = month;
31     }
32     public double getSalary(int month){
33         return 0;
34     }          
35  
36 }
 1 public class Hourlyemployee extends Colaemployee {
 2  
 3     int hourmoney;
 4     int worktime;
 5     public Hourlyemployee() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9  
10     public Hourlyemployee(int hourmoney, int worktime) {
11         super();
12         this.hourmoney = hourmoney;
13         this.worktime = worktime;
14     }
15     public double getSalary(int month) {
16         if (super.getMonth() > month) {
17             if (worktime > 160) {
18                 return hourmoney * 160 + hourmoney * (worktime - 160) * 1.5
19                         + 100;
20             } else {
21                 return hourmoney * worktime + 100;
22  
23            }
24        } else {
25             if (worktime > 160) {
26                 return hourmoney * 160 + hourmoney * (worktime - 160) * 1.5;
27             } else {
28                return hourmoney * worktime;
29  
30            }
31        }
32    }
33  
34  
35      
36 }
 1 public class SalariedEmployee extends Colaemployee{
 2  
 3     int monthmoney;
 4  
 5     public SalariedEmployee() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9  
10     public SalariedEmployee(int monthmoney) {
11         super();
12         this.monthmoney = monthmoney;
13     }
14  
15     public double getSalary(int month){
16         if (super.getMonth()==month){
17             return monthmoney;
18         }
19         else{
20             return monthmoney;
21         }
22     }
23  
24      
25      
26  
27 }
 1 public class SalesEmployee extends Colaemployee {
 2  
 3     int xiaoshoue;
 4     double tichenglv;
 5     public SalesEmployee() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9     public SalesEmployee(int xiaoshoue, double tichenglv) {
10         super();
11         this.xiaoshoue = xiaoshoue;
12         this.tichenglv = tichenglv;
13     }
14     public double getSalary(int month) {
15         if (super.getMonth() == month) {
16             return xiaoshoue * tichenglv + 100;
17         } else {
18             return xiaoshoue * tichenglv;
19         }
20     }
21      
22 }
1 public class Company {
2      public void getSalary(Colaemployee c, int month) {
3          System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month)+ "元");
4          }      
5 }      
 1 public class Textcompany {
 2  
 3     public static void main(String[] args) {
 4      
 5         Colaemployee[] cel = {
 6                  new salariedemployee("salariedEmployee", 6, 30000),
 7                  new hourlyEmployee("hourlyEmployee", 5, 100, 300),
 8                  new SalesEmploye("salesEmployee", 3, 500000, 0.3)
 9          };
10          
11         for (int i = 0; i < cel.length; i++) {
12             new Company().getSalary(cel[i], 10);
13          }
14   
15  
16     }
17  
18 }

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

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

 

posted @ 2021-07-14 13:43  唐一南  阅读(87)  评论(0)    收藏  举报