第十五周作业

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 第十五周作业;

public abstract class ColaEmployee {
    String employeeName;
    int employeeMonth;
    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    public int getEmployeeMonth() {
        return employeeMonth;
    }
    public void setEmployeeMonth(int employeeMonth) {
        this.employeeMonth = employeeMonth;
    }
    public abstract double getSalary(int month);
    public String getEmployeename(){
        return employeeName;
    }
    public ColaEmployee(String employeeName, int employeeMonth) {
        super();
        this.employeeName = employeeName;
        this.employeeMonth = employeeMonth;
    }
    
    
    

}
package 第十五周作业;

public class HourlyEmployee extends ColaEmployee {
    double hSales;
    double mHours;
    
    public double gethSales() {
        return hSales;
    }
    public void sethSales(double hSales) {
        this.hSales = hSales;
    }
    public double getmHours() {
        return mHours;
    }
    public void setmHours(double mHours) {
        this.mHours = mHours;
    }
    public HourlyEmployee(String employeeName, int employeeMonth,
            double hSales, double mHours) {
        super(employeeName, employeeMonth);
        this.hSales = hSales;
        this.mHours = mHours;
    }
    public double getSalary(int month){
        if(super.employeeMonth==month){
            if(mHours>160){
                return hSales*160+hSales*(mHours-160)*1.5+100;
            }else{
                return hSales*mHours+100;
            }
        }else{
            if(mHours>160){
                return hSales*160+mHours*(mHours-160)*1.5;
            }else{
                return hSales*mHours;
            }
        }
    }

}

 


package 第十五周作业;

public class SalariedEmployee extends ColaEmployee {
    double monthlySalary;

    public double getMonthlysalary() {
        return monthlySalary;
    }
    public SalariedEmployee(String employeeName, int employeeMonth,
            double monthlySalary) {
        super(employeeName, employeeMonth);
        this.monthlySalary = monthlySalary;
    }


    @Override
    public double getSalary(int month) {
        // TODO Auto-generated method stub
        if(month==super.employeeMonth){
            return (monthlySalary+100);
        }else{
            return monthlySalary;
        }
    }
       
    

}
package 第十五周作业;

public class salesEmployee extends ColaEmployee{
    double msales;
    double rRate;
    
    public double getMsales() {
        return msales;
    }
    public void setMsales(double msales) {
        this.msales = msales;
    }
    public double getrRate() {
        return rRate;
    }
    public void setrRate(double rRate) {
        this.rRate = rRate;
    }
    public salesEmployee(String employeeName, int employeeMonth, double msales,
            double rRate) {
        super(employeeName, employeeMonth);
        this.msales = msales;
        this.rRate = rRate;
    }
    public double getSalary(int month){
        if(super.employeeMonth==month){
            return msales*rRate+100;
        }else{
            return msales*rRate;
        }
    }
    

}
package 第十五周作业;

public class Company  {
    public void getSalary(ColaEmployee c,int month){
        System.out.println(c.employeeName+month+"月的月薪是"+c.getSalary(month));
    }

}
package 第十五周作业;

public class TestCompany {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColaEmployee c[]={new SalariedEmployee("张三",3,5000),
                new HourlyEmployee("李四",4,12,300),
                new salesEmployee("王五",5,80000,0.5)};
         for (int i = 0; i < c.length; i++) {
               new Company().getSalary(c[i], 12);
         }
        

    }

}

posted @ 2021-06-17 15:50  聂芳芳  阅读(63)  评论(0)    收藏  举报