公司员工工资父类与子类

Employee

package com.hp.demo11.demo06;

public abstract class Employee  {
    private String name;
    private int yf;
    public abstract double salary(int yf);

    public Employee() {
    }

    public Employee(String name, int yf) {
        this.name = name;
        this.yf = yf;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getYf() {
        return yf;
    }

    public void setYf(int yf) {
        this.yf = yf;
    }
}

SalariedEmployee

package com.hp.demo11.demo06;

public class SalariedEmployee extends Employee {
    private double yx;//月薪

    public SalariedEmployee(double yx) {
        this.yx = yx;
    }

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

    public double getYx() {
        return yx;
    }

    public void setYx(double yx) {
        this.yx = yx;
    }

    @Override
    public double salary(int yf) {

        double salary = this.yx;
        if (super.getYf() == yf) {
            salary += 100;
            System.out.println(super.getName()+"因为过生日,奖励100");
        }
        return salary;
    }
}

HourlyEmployee

package com.hp.demo11.demo06;

public class HourlyEmployee extends Employee{
    private double xsg;//工作薪资
    private int xss;//工作时长
    public HourlyEmployee(double xsg,int xss){
        this.xsg = xsg;
        this.xss = xss;
    }

    public HourlyEmployee(String name, int yf, double xsg, int xss) {
        super(name, yf);
        this.xsg = xsg;
        this.xss = xss;
    }

    public double getXsg() {
        return xsg;
    }

    public void setXsg(double xsg) {
        this.xsg = xsg;
    }

    public int getXss() {
        return xss;
    }

    public void setXss(int xss) {
        this.xss = xss;
    }

    @Override
    public double salary(int yf) {
        double salary  =0 ;
        //工作时长超过160小时
        if (xss > 160){
            salary = 160 * this.xsg + (this.xss- 160)*1.5*this.xsg;
        }
        //是否过生日
        if (super.getYf() == yf) {
            salary +=100;
            System.out.println(super.getName()+"因为过生日,奖励100");
        }
        return salary;
    }
}

SalesEmployee

package com.hp.demo11.demo06;

public class SalesEmployee extends Employee {
    private double sales;//销售额
    private double rate;//提成

    public SalesEmployee(double sales, double rate) {
        this.sales = sales;
        this.rate = rate;
    }

    public SalesEmployee(String name, int yf, double sales, double rate) {
        super(name, yf);
        this.sales = sales;
        this.rate = rate;
    }


    public SalesEmployee() {
    }

    public double getSales() {
        return sales;
    }

    public void setSales(double sales) {
        this.sales = sales;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    @Override
    public double salary(int yf) {
        double salary = this.sales * this.rate;
        if (super.getYf()== yf) {
            salary += 100;
            System.out.println(super.getName()+"因为过生日,奖励100");
        }
        return salary;
    }
}

BasePlusSalesEmployee

package com.hp.demo11.demo06;

public class BasePlusSalesEmployee extends SalesEmployee{
  private double dixin;//底薪

    public double getDixin() {
        return dixin;
    }

    public void setDixin(double dixin) {
        this.dixin = dixin;
    }


    public BasePlusSalesEmployee(String name, int yf, double sales, double rate, double dixin) {
        super(name, yf, sales, rate);
        this.dixin = dixin;
    }

    public BasePlusSalesEmployee(double dixin) {
        this.dixin = dixin;
    }

    @Override
    public double salary(int yf) {
        double zong = dixin+super.getRate() * super.getSales();
        if (super.getYf() == yf) {
            zong +=100;
            System.out.println(super.getName()+"因为过生日,奖励100");
        }
        return zong;
    }
}

Tex

package com.hp.demo11.demo06;

public class Tex {
    public static void main(String[] args) {
       Employee[] employees = new Employee[10];
       employees[0] = new SalariedEmployee("lll", 8, 1000);
       employees[1] = new HourlyEmployee("iii", 5, 80, 180);
       employees[2] = new SalesEmployee("ppp", 8, 80, 100);
       employees[3] = new BasePlusSalesEmployee("llll", 8, 80, 15, 1500);
        for (int i = 0; i <employees.length ; i++) {
            if (employees[i] != null) {
                double salary =employees[i].salary(8);
                System.out.println(employees[i].getName()+"工资为"+salary);
            }
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
```java // 定义员工类 class Employee { private String name; private double baseSalary; public Employee(String name, double baseSalary) { this.name = name; this.baseSalary = baseSalary; } public String getName() { return name; } public double getBaseSalary() { return baseSalary; } public double calculateSalary() { return baseSalary; } } // 定义经理类,继承员工类 class Manager extends Employee { private double bonus; public Manager(String name, double baseSalary, double bonus) { super(name, baseSalary); this.bonus = bonus; } @Override public double calculateSalary() { return getBaseSalary() + bonus; } } // 定义销售人员类,继承员工类 class SalesPerson extends Employee { private double commission; public SalesPerson(String name, double baseSalary, double commission) { super(name, baseSalary); this.commission = commission; } @Override public double calculateSalary() { return getBaseSalary() + commission; } } // 定义普通员工类,继承员工类 class OrdinaryEmployee extends Employee { public OrdinaryEmployee(String name, double baseSalary) { super(name, baseSalary); } } // 定义测试类 class Test { public static void printSalary(Employee employee) { System.out.println("员工姓名:" + employee.getName()); System.out.println("员工薪水:" + employee.calculateSalary()); } public static void main(String[] args) { // 创建不同岗位的员工对象 Manager manager = new Manager("经理A", 5000, 2000); SalesPerson salesPerson = new SalesPerson("销售人员B", 3000, 1000); OrdinaryEmployee ordinaryEmployee = new OrdinaryEmployee("普通员工C", 2000); // 输出员工薪水 printSalary(manager); printSalary(salesPerson); printSalary(ordinaryEmployee); } } ``` 运行结果: ``` 员工姓名:经理A 员工薪水:7000.0 员工姓名:销售人员B 员工薪水:4000.0 员工姓名:普通员工C 员工薪水:2000.0 ``` 以上是一个简单的实现,根据不同岗位的员工类进行薪水计算,并通过测试类进行输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值