JAVA第十五周作业0611

4、 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 数组里,并单元出数组中每个员工当月的 工资.

package com.homework01501;

public abstract class ColaEmployee {
    protected String name;        
    protected int staffMonth;
    protected int month;
    
    public void getSalary(int month) {
        if(staffMonth == month){
            System.out.println("额外奖励100 元");
        }
    }

    public ColaEmployee(String name, int staffMonth, int month) {
        super();
        this.name = name;
        this.staffMonth = staffMonth;
        this.month = month;
    }

    public ColaEmployee() {
        super();
    }


    
}
package com.homework01501;

public class Company {
    SalariedEmployee Salaried = new SalariedEmployee();
    hourlyEmployee hourly = new hourlyEmployee();
    SalesEmployee Sales = new SalesEmployee();
    
    public void  show(String name, int staffMonth, int month,int monthSalary) {
        Salaried.name = name;
        Salaried.staffMonth = staffMonth;
        Salaried.month = month;
        Salaried.monthSalary = monthSalary;
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (monthSalary +100));
        else
            System.out.println(name + month +"月的工资为"+ monthSalary);
    }
    public void  show(String name, int staffMonth, int month,int hourlySalary, int monthHourly) {
        hourly.name = name;
        hourly.staffMonth = staffMonth;
        hourly.month = month;
        hourly.hourlySalary = hourlySalary;
        hourly.monthHourly = monthHourly;
        double Salary;
        Salary = hourlySalary *monthHourly;
        if(monthHourly >160)
            Salary = hourlySalary *(160+(monthHourly-160)*1.5);
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (Salary +100));
        else
            System.out.println(name + month +"月的工资为"+ Salary);
    }
    public void  show(String name, int staffMonth, int month,int monthlySalesVolume, double royaltyRate) {
        Sales.name = name;
        Sales.staffMonth = staffMonth;
        Sales.month = month;
        Sales.monthlySalesVolume = monthlySalesVolume;
        Sales.royaltyRate = royaltyRate;
        double Salary;
        Salary = monthlySalesVolume * royaltyRate;
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (Salary +100));
        else
            System.out.println(name + month +"月的工资为"+ Salary);
    }
}
package com.homework01501;

public class hourlyEmployee extends ColaEmployee {
    protected int hourlySalary;    //时薪
    protected int monthHourly;        //时长
    

    public hourlyEmployee(String name, int staffMonth, int month,
            int hourlySalary, int monthHourly) {
        super(name, staffMonth, month);
        this.hourlySalary = hourlySalary;
        this.monthHourly = monthHourly;
        double Salary;
        Salary = hourlySalary *monthHourly;
        if(monthHourly >160)
            Salary = hourlySalary *(160+(monthHourly-160)*1.5);
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (Salary +100));
        else
            System.out.println(name + month +"月的工资为"+ Salary);
    }


    public hourlyEmployee() {
        
    }
    

}
package com.homework01501;

public class SalariedEmployee extends ColaEmployee{
    protected int monthSalary;    //月薪

    public SalariedEmployee(String name, int staffMonth, int month,
            int monthSalary) {
        super(name, staffMonth, month);
        this.monthSalary = monthSalary;
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (monthSalary +100));
        else
            System.out.println(name + month +"月的工资为"+ monthSalary);
    }

    public SalariedEmployee() {
        
    }
    

    
    
}
package com.homework01501;

public class SalesEmployee extends ColaEmployee {
    protected int monthlySalesVolume;    //月销售额
    protected double royaltyRate;        //提成率
    public SalesEmployee(String name, int staffMonth, int month,
            int monthlySalesVolume, double royaltyRate) {
        super(name, staffMonth, month);
        this.monthlySalesVolume = monthlySalesVolume;
        this.royaltyRate = royaltyRate;
        double Salary;
        Salary = monthlySalesVolume * royaltyRate;
        if(staffMonth == month)
            System.out.println(name + month +"月的工资为"+ (Salary +100));
        else
            System.out.println(name + month +"月的工资为"+ Salary);
    }
    public SalesEmployee() {
    
    }
    
    
    
    
}
package com.homework01501;

public class TestCompany {

    public static void main(String[] args) {

        Company c = new Company();
        c.show("李四", 3, 2, 5000);
        c.show("月薪", 5, 2, 161, 15);
        c.show("销售", 2, 2, 20000, 0.15);
        ColaEmployee[] y = new ColaEmployee[3];
        y[0] = new SalariedEmployee("王武", 2, 2, 5000);
        y[1] = new hourlyEmployee("小六", 5, 2, 160, 12);
        y[2] = new SalesEmployee("小张", 4, 4, 56000, 0.15);
    }

}

 

 

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

 package com.homework01502; public class Apple { public Apple() { System.out.println("创建了一个苹果类对象"); } } 

 package com.homework01502; public class Banana { public Banana() { System.out.println("创建了一个香蕉类对象"); } } 

 package com.homework01502; public class Grape { public Grape() { System.out.println("创建了一个葡萄类对象"); } } 

package com.homework01502;

import java.util.Scanner;

public class Gradener {
    public Fruit fruit(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一种水果名称");
        String name = input.next();
        Fruit fruit = null;
        if(name.equals("苹果")) {
            return fruit = (Fruit) new Apple();
        }else if(name.equals("香蕉")){
            return fruit = (Fruit) new Banana();
        }else if(name.equals("葡萄")){
            return fruit = (Fruit) new Banana();
        }else
            System.out.println("暂时无法种植");
        return fruit;
    }
}

 package com.homework01502; public interface Fruit { } 

package com.homework01502;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Gradener g = new Gradener();
        g.fruit();
    }

}
package com.homework01502;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Gradener g = new Gradener();
        g.fruit();
    }

}

 

posted @ 2021-06-18 18:22  L'童话故事  阅读(68)  评论(0编辑  收藏  举报