Java输出某公司的雇员月薪

1. (考察类的继承与方法的重写)

class Employee {
    private String name;
    private int birthMonth;

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

    public double getSalary(int month) {
        if (birthMonth == month) {
            return 100;
        } else {
            return 0;
        }
    }
}

class SalariedEmployee extends Employee {
    private float monthSalary;

    public SalariedEmployee(String name, int birthMonth, float monthSalary) {
        super(name, birthMonth);
        this.monthSalary = monthSalary;
    }

    public double getSalary(int month) {
        return super.getSalary(month) + monthSalary;
    }
}

class HourlyEmployee extends Employee {
    private double hourSalary;
    private int workHours;

    public HourlyEmployee(String name, int birthMonth, double hourSalary, int workHours) {
        super(name, birthMonth);
        this.hourSalary = hourSalary;
        this.workHours = workHours;
    }

    public double getSalary(int month) {
        if (workHours <= 160) {
            return super.getSalary(month) + hourSalary * workHours;
        } else {
            return super.getSalary(month) + hourSalary * workHours + 1.5 * hourSalary * (workHours - 160);
        }
    }
}

class SalesEmployee extends Employee {
    private double monthSales;
    private double royaltyRate;

    public SalesEmployee(String name, int birthMonth, double monthSales, double royaltyRate) {
        super(name, birthMonth);
        this.monthSales = monthSales;
        this.royaltyRate = royaltyRate;
    }

    @Override
    public double getSalary(int month) {
        return super.getSalary(month) + monthSales * (1 + royaltyRate);
    }
}

class BasePlusSalesEmployee extends SalesEmployee {
    private double basicSalary;

    public BasePlusSalesEmployee(String name, int birthMonth, double monthSales, double royaltyRate, double basicSalary) {
        super(name, birthMonth, monthSales, royaltyRate);
        this.basicSalary = basicSalary;
    }

    @Override
    public double getSalary(int month) {
        return super.getSalary(month) + basicSalary;
    }
}

public class mm {
    public static void main(String[] args) {
        Employee[] employees = new Employee[4];
        employees[0] = new SalariedEmployee("李华", 8, 3000);
        employees[1] = new HourlyEmployee("陈岩", 3, 30, 168);
        employees[2] = new SalesEmployee("吕布", 6, 500, 0.2);
        employees[3] = new BasePlusSalesEmployee("赵四", 10, 170, 0.2, 3000);
        int targetMonth = 10;
        for (Employee employee : employees) {
            System.out.println(employee.getSalary(targetMonth));
        }
    }
}

分为以下若干类:

(1) Employee:这是所有员工总的父类。

① 属性:员工的姓名,员工的生日月份

② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。

① 属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定(工资=销售额*(1+提成率))。

① 属性:月销售额、提成率。

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分(工资=底薪+销售额*(1+提成率))。

  •  属性:底薪。

要求:

在main方法中创建一个Employee 数组,分别创建若干不同子类的Employee对象,并打印某个月的工资。

注意:要求把每个类都做成完全封装,不允许非私有化属性。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值