第十五周作业

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 a;

public abstract class ColaEmployee {
    protected String xm;
    protected int sryf;
    public ColaEmployee() {
        super();
    }
    public ColaEmployee(String xm, int sryf) {
        super();
        this.xm = xm;
        this.sryf = sryf;
    }
    public abstract double getSalary(int month); 

}
package a;

public class SalariedEmployee extends ColaEmployee{
    private double yx;

    public SalariedEmployee(String xm, int sryf) {
        super(xm,sryf);
    }

    public SalariedEmployee(String xm, int sryf,double yx) {
        super(xm,sryf);
        this.yx = yx;
    }

    @Override
    public double getSalary(int month) {
        if(this.sryf==month) {
            return yx+100;
        }else {
            return yx;
        }
    }

}
package a;

public class HourlyEmployee extends ColaEmployee{
    private int xsgz;
    private int gzs;
    public HourlyEmployee(String xm, int sryf) {
        super(xm,sryf);
    }

    public HourlyEmployee(String xm, int sryf,int xsgz,int gzs) {
        super(xm,sryf);
        this.xsgz = xsgz;
        this.gzs=gzs;
    }

    @Override
    public double getSalary(int month) {
        if(this.sryf==month) {
            if(gzs>160) {
                return (gzs-160)*(1.5*xsgz)+(160*xsgz)+100;
            }else {
                return (gzs*xsgz)+100;
            }
        }else {
            if(gzs>160) {
                return (gzs-160)*(1.5*xsgz)+(160*xsgz);
            }else {
                return (gzs*xsgz);
            }
        }
    }

}
package a;

public class SalesEmployee extends ColaEmployee{
    private double yxse;
    private double tcl;

    public SalesEmployee(String xm, int sryf) {
        super(xm,sryf);
    }

    public SalesEmployee(String xm, int sryf,double yxse, double tcl) {
        super(xm,sryf);
        this.yxse = yxse;
        this.tcl = tcl;
    }

    @Override
    public double getSalary(int month) {
        if(this.sryf==month) {
            return yxse*tcl+yxse+100;
        }else {
            return yxse*tcl+yxse;
        }
        
    }

}
package a;

public class Company {

    public void show(ColaEmployee c,int month) {
        System.out.println(c.getSalary(month));
        
    }

}
package a;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Company c = new Company();
        ColaEmployee c1 = new SalariedEmployee("n", 12, 100);
        c.show(c1, 12);
        SalesEmployee c2 = new SalesEmployee("n", 11, 100, 0.3);
        SalariedEmployee c3 = new SalariedEmployee("n", 12, 100);
        ColaEmployee c4 = new HourlyEmployee("n", 12, 100,20);

        ColaEmployee ccc[] = {
                c2,c3,c4
        };
        for(int a=0;a<3;a++) {
            c.show(ccc[a], 11);
        }
        
    }

}

 

posted @ 2021-06-14 14:59  王玉宁325  阅读(69)  评论(0编辑  收藏  举报