Java基础入门--面向对象课后题(2)

本文描述了一个Java程序,使用面向对象的方式设计了Employee、SalariedEmployee、HourlyEmployee、SalesEmployee和BasePlusSalesEmployee五个类,实现了员工工资的计算逻辑,并在Example17测试类中创建这些类的对象并打印工资。
摘要由CSDN通过智能技术生成

某公司的雇员分为5类,每类员工都有相应的封装类,这5个类的信息如下所示。
(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。

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

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
① 属性:月销售额、提成率。

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
① 属性:底薪。

本题要求根据上述雇员分类,编写一个程序,实现以下功能:
(1)创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。
(2)每个类都完全封装,不允许非私有化属性。

完整代码在最后

1 Employee

(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

//(1) Employee:这是所有员工总的父类。
//① 属性:员工的姓名,员工的生日月份
//② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
abstract class Employee {
    private String name;    //定义姓名,并私有化属性
    private int month;      //定义生日月份,私有化属性

    public Employee() {
        // 无参构造器
    }

    public Employee(String name, int month) {        //有参构造
        this.name = name;
        this.month = month;
    }

    //获取属性name的方法
    public String getName() {
        return name;
    }

    //给属性name赋初始值
    public void setName(String name) {
        this.name = name;
    }

    //获取属性month的方法
    public int getMonth() {
        return month;
    }

    //给属性month赋初始值
    public void setMonth(int month) {
        this.month = month;
    }

    //方法:getSalary(int month)
    public double getSalary(int month) {
        double salary = 0;    //定义工资变量
        //判断当前月份是否是员工的生日月份,如是奖励100元
        if (this.month == month) {
            salary += 100;
        }
        return salary;      //返回工资salary
    }
}

2 SalariedEmployee

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。

//(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
//① 属性:月薪。
class SalariedEmployee extends Employee {
    private double monthSalary;     //封装月薪monthSalary属性

    public SalariedEmployee() {
        //无参构造法
    }

    //有参构造法
    public SalariedEmployee(String name, int month, double monthSalary) {
        super(name, month);      //调用父类有参构造方法
        this.monthSalary = monthSalary;     //给属性monthSalary初始化赋值
    }

    //获取monthSalary的值
    public double getMonthSalary() {
        return monthSalary;
    }

    //给monthSalary赋值
    public void setMonthSalary(double monthSalary) {
        this.monthSalary = monthSalary;
    }
    //覆盖父类中的方法

    @Override
    public double getSalary(int month) {
        double salary = monthSalary + super.getSalary(month);    //重新定义工资变量,月工资加生日奖金
        return salary;
    }
}

3 HourlyEmployee

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

//(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。
//① 属性:每小时的工资、每月工作的小时数。
class HourlyEmployee extends Employee {
    private double hourlySalary;    //定义属性hourlySalary每小时的工资
    private int hours;  //定义属性hours每月工作的小时数

    public HourlyEmployee() {
    }

    public HourlyEmployee(String name, int month, double hourlySalary, int hours) {
        super(name, month);  //调用父类有参构造方法
        this.hourlySalary = hourlySalary;     //为属性hourlySalary初始化赋值
        this.hours = hours;       //为属性hours 初始化赋值
    }

    //获取hourSalary 每小时工资的值
    public double getHourlySalary() {
        return hourlySalary;
    }

    //给hourSalary 每小时工资赋值
    public void setHourlySalary(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }

    //获取hours每月工作的小时数的值
    public int getHours() {
        return hours;
    }

    //给hours每月工作的小时数赋值
    public void setHours(int hours) {
        this.hours = hours;
    }
    //覆盖父类方法

    @Override
    public double getSalary(int month) {
        //工作小时数异常
        if (hours < 0) {
            System.out.println("数据错误!");
            return 0;
        }
        //工作时间小于160
        else if (hours <= 160) {
            return hourlySalary * hours + super.getSalary(month);
        }
        //超过160的部分  1.5倍工资
        else
            return hourlySalary * 160 + 1.5 * hourlySalary * (hours - 160) + super.getSalary(month);
    }
}

4 SalesEmployee

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
① 属性:月销售额、提成率。

//(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
//① 属性:月销售额、提成率。
class SalesEmployee extends Employee {
    private double sales;      //定义销售额sales
    private double rate;    //定义提成率rate

    public SalesEmployee() {
    }

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

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

    //重写父类方法
    //(1+提成率)*原销售额+其他
    @Override
    public double getSalary(int month) {
        return this.getSales() * (1 + this.getRate()) + super.getSalary(month);
    }
}

5 BasePlusSalesEmployee

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
① 属性:底薪。

//(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
//① 属性:底薪。
class BasePlusSalesEmployee extends SalesEmployee {
    private double baseSalary;      //定义基础工资baseSalary

    public BasePlusSalesEmployee() {
    }

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

    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    //重写父类方法
    @Override
    public double getSalary(int month) {
        return baseSalary + super.getSalary(month);
    }
}

6 测试类 Example17

public class Example17 {
    public static void main(String[] args) {
        //声明一个Employee类型的数组,并创建不同子类型的对象
        Employee[] employee = {
                new SalariedEmployee("张三", 1, 6000),
                new HourlyEmployee("李四", 2, 50, 180),
                new SalesEmployee("王五", 3, 6500, 0.15),
                new BasePlusSalesEmployee("赵六", 4, 5000, 0.15, 2000)
        };
        //打印每个员工的工资
        for (int i = 0; i < employee.length; i++) {
            System.out.println(Math.round(employee[i].getSalary(10)));
        }

    }

}


在这里插入图片描述

7 完整代码

//(1) Employee:这是所有员工总的父类。
//① 属性:员工的姓名,员工的生日月份
//② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
abstract class Employee {
    private String name;    //定义姓名,并私有化属性
    private int month;      //定义生日月份,私有化属性

    public Employee() {
        // 无参构造器
    }

    public Employee(String name, int month) {        //有参构造
        this.name = name;
        this.month = month;
    }

    //获取属性name的方法
    public String getName() {
        return name;
    }

    //给属性name赋初始值
    public void setName(String name) {
        this.name = name;
    }

    //获取属性month的方法
    public int getMonth() {
        return month;
    }

    //给属性month赋初始值
    public void setMonth(int month) {
        this.month = month;
    }

    //方法:getSalary(int month)
    public double getSalary(int month) {
        double salary = 0;    //定义工资变量
        //判断当前月份是否是员工的生日月份,如是奖励100元
        if (this.month == month) {
            salary += 100;
        }
        return salary;      //返回工资salary
    }
}

//(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
//① 属性:月薪。
class SalariedEmployee extends Employee {
    private double monthSalary;     //封装月薪monthSalary属性

    public SalariedEmployee() {
        //无参构造法
    }

    //有参构造法
    public SalariedEmployee(String name, int month, double monthSalary) {
        super(name, month);      //调用父类有参构造方法
        this.monthSalary = monthSalary;     //给属性monthSalary初始化赋值
    }

    //获取monthSalary的值
    public double getMonthSalary() {
        return monthSalary;
    }

    //给monthSalary赋值
    public void setMonthSalary(double monthSalary) {
        this.monthSalary = monthSalary;
    }
    //覆盖父类中的方法

    @Override
    public double getSalary(int month) {
        double salary = monthSalary + super.getSalary(month);    //重新定义工资变量,月工资加生日奖金
        return salary;
    }
}

//(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。
//① 属性:每小时的工资、每月工作的小时数。
class HourlyEmployee extends Employee {
    private double hourlySalary;    //定义属性hourlySalary每小时的工资
    private int hours;  //定义属性hours每月工作的小时数

    public HourlyEmployee() {
    }

    public HourlyEmployee(String name, int month, double hourlySalary, int hours) {
        super(name, month);  //调用父类有参构造方法
        this.hourlySalary = hourlySalary;     //为属性hourlySalary初始化赋值
        this.hours = hours;       //为属性hours 初始化赋值
    }

    //获取hourSalary 每小时工资的值
    public double getHourlySalary() {
        return hourlySalary;
    }

    //给hourSalary 每小时工资赋值
    public void setHourlySalary(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }

    //获取hours每月工作的小时数的值
    public int getHours() {
        return hours;
    }

    //给hours每月工作的小时数赋值
    public void setHours(int hours) {
        this.hours = hours;
    }
    //覆盖父类方法

    @Override
    public double getSalary(int month) {
        //工作小时数异常
        if (hours < 0) {
            System.out.println("数据错误!");
            return 0;
        }
        //工作时间小于160
        else if (hours <= 160) {
            return hourlySalary * hours + super.getSalary(month);
        }
        //超过160的部分  1.5倍工资
        else
            return hourlySalary * 160 + 1.5 * hourlySalary * (hours - 160) + super.getSalary(month);
    }
}

//(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
//① 属性:月销售额、提成率。
class SalesEmployee extends Employee {
    private double sales;      //定义销售额sales
    private double rate;    //定义提成率rate

    public SalesEmployee() {
    }

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

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

    //重写父类方法
    //(1+提成率)*原销售额+其他
    @Override
    public double getSalary(int month) {
        return this.getSales() * (1 + this.getRate()) + super.getSalary(month);
    }
}

//(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
//① 属性:底薪。
class BasePlusSalesEmployee extends SalesEmployee {
    private double baseSalary;      //定义基础工资baseSalary

    public BasePlusSalesEmployee() {
    }

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

    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    //重写父类方法
    @Override
    public double getSalary(int month) {
        return baseSalary + super.getSalary(month);
    }
}

//定义一个测试类
public class Example17 {
    public static void main(String[] args) {
        //声明一个Employee类型的数组,并创建不同子类型的对象
        Employee[] employee = {
                new SalariedEmployee("张三", 1, 6000),
                new HourlyEmployee("李四", 2, 50, 180),
                new SalesEmployee("王五", 3, 6500, 0.15),
                new BasePlusSalesEmployee("赵六", 4, 5000, 0.15, 2000)
        };
        //打印每个员工的工资
        for (int i = 0; i < employee.length; i++) {
            System.out.println(Math.round(employee[i].getSalary(10)));
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值