继承方法计算不同类型工资

176 阅读2分钟
某公司的雇员分为以下若干类:
    Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:void getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励1000元。
    SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪
    HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
    SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
    BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
package gongzi;
/**
 * 总父类
 * @author Administrator
 *
 */
package gongzi;

public class Employee {
    /**
     * 员工总类
     */
    public String name;
    public int month;//生日月份
    public void getSalary(int month){
    }
}

package gongzi;
/**
* 固定工资员工
* @author Administrator
*
*/
public class SalariedEmployee extends Employee {
public double money;//固定工资数;

@Override
public void getSalary(int month) {
    if(this.month==month){
        System.out.println(this.name+""+month+"月份的工资为"+(money+1000)+"元");
        }
    else{
        System.out.println(this.name+""+month+"月份的工资为"+money+"元");
    }
}

}“

package gongzi;
/**
 * 小时工
 * @author Administrator
 *
 */
public class HourlyEmployee extends Employee {
    public double money;//每小时工资
    public double hour;//每月工作的小时数
    @Override
    public void getSalary(int month) {
        double salary=0;
        if(hour > 160){
            salary = (160*money)+(hour-160)*1.5*money;
        }else{
            salary = hour*money;
        }
        if(this.month==month){
            salary+=1000;
        }
        System.out.println(this.name+""+month+"月份的工资为"+salary+"元");
    }


}
package gongzi;
/**
 * 无底薪销售
 * @author Administrator
 *
 */
public class SalesEmployee extends Employee {
    public double money;//月销售额
    public double tcl;//提成率
    @Override
    public void getSalary(int month) {
        if(this.month == month){
            System.out.println(this.name+""+month+"月份的工资为"+(money*tcl+1000)+"元");
        }else{
            System.out.println(this.name+""+month+"月份的工资为"+(money*tcl)+"元");
        }
    }


}
package gongzi;
/**
 * 有底薪销售
 * @author Administrator
 *
 */
public class BasePlusSalesEmployee extends SalesEmployee {
    public double dx;//底薪

    @Override
    public void getSalary(int month) {
        if(this.month==month){
            System.out.println(this.name+month+"月工资为:"+(dx+this.money*tcl+1000)+"元");
        }
        else{
            System.out.println(this.name+month+"月工资为:"+(dx+this.money*tcl)+"元");
        }
    }


}
package gongzi;
/**
 * 测试类
 * @author Administrator
 *
 */
public class Test {
    public static void main(String[] args) {
        for(int i=1;i<=12;i++){
        System.out.println("-----------------"+i+"月份工资表"+"-----------------");
        SalariedEmployee e1=new SalariedEmployee();
        e1.month=2;
        e1.name="张三";
        e1.money=3000;
        e1.getSalary(i);
        HourlyEmployee e2 = new HourlyEmployee();
        e2.name = "李四";
        e2.month = 2;
        e2.money = 10;
        e2.hour = 160;
        e2.getSalary(i);

        SalesEmployee e3 = new SalesEmployee();
        e3.name = "王五";
        e3.month = 3;
        e3.money = 100000;
        e3.tcl = 0.1;
        e3.getSalary(i);

        BasePlusSalesEmployee e4 = new BasePlusSalesEmployee();
        e4.name = "赵六";
        e4.month = 4;
        e4.money = 100000;
        e4.tcl = 0.1;
        e4.dx = 2000;
        e4.getSalary(i);
        }
    }

}