二、实验内容 某公司的雇员分为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)每个类都完全封装,不允许非私有化属性。 |
四、实验步骤(包括主要步骤、代码分析等) 参考代码之主函数 /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion��2022.12.20
Last edited: 2022.12.20
*/
public class Main {
public static void main(String[] args) {
Employee salariedEmployee = new SalariedEmployee("张三", 5, 1000);
Employee salesEmployee = new SalesEmployee("李四", 4, 10, 200);
Employee hourlyEmloyee = new HourlyEmloyee("王五", 5, 15, 100);
Employee basePlusSalesEmployee = new BasePlusSalesEmployee("赵六", 5, 10, 200, 500);
Employee arr[] = { salariedEmployee, salesEmployee, hourlyEmloyee, basePlusSalesEmployee };
for (Employee employee : arr) {
System.out.println(employee.getName() + "五月薪:" + employee.getSalary(5));
// 张三生日5月,月薪1000,五月加100,所以月薪1100,输出正确,之后同理,不再做统一阐述。
System.out.println("-------------------");
}
}
}
参考代码之Employee /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion��2022.12.20
Last edited: 2022.12.20
*/
abstract class Employee {
private String name;
private int birthday;
public double getSalary(int month) {
return this.birthday == month ? 100 : 0;
}
public Employee() {
super();
}
public Employee(String name, int birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public int getBirthday() {
return birthday;
}
public void setBirthday(int birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
参考代码之SalariedEmployee /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion��2022.12.20
Last edited: 2022.12.20
*/
/**
* 拿固定工资的员工
*
*
*
*/
public class SalariedEmployee extends Employee {
private double salar;
public SalariedEmployee(String name, int birthday, double salar) {
super(name, birthday);
this.salar = salar;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + salar;
}
public double getSalar() {
return salar;
}
public void setSalar(double salar) {
this.salar = salar;
}
}
参考代码之HourlyEmployee /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion??2022.12.20
Last edited: 2022.12.20
*/
/**
* 拿小时工资的员工
*
* @author 18118
*
*/
public class HourlyEmloyee extends Employee {
private double hourSalar;
private double hour;
public HourlyEmloyee(String name, int birthday, double hourSalar, double hour) {
super(name, birthday);
this.hourSalar = hourSalar;
this.hour = hour;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + hour * hourSalar;
}
public double getHourSalar() {
return hourSalar;
}
public void setHourSalar(double hourSalar) {
this.hourSalar = hourSalar;
}
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
}
参考代码之SalesEmployee /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion��2022.12.20
Last edited: 2022.12.20
*/
/**
* 销售
*
*
*
*/
public class SalesEmployee extends Employee {
private double saleNum;
private double plusRate;
public SalesEmployee(String name, int birthday, double saleNum, double plusRate) {
super(name, birthday);
this.saleNum = saleNum;
this.plusRate = plusRate;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + saleNum * plusRate;
}
public double getSaleNum() {
return saleNum;
}
public void setSaleNum(double saleNum) {
this.saleNum = saleNum;
}
public double getPlusRate() {
return plusRate;
}
public void setPlusRate(double plusRate) {
this.plusRate = plusRate;
}
}
参考代码之BaseplusEmplyee /*
original author: jacky Li
Email : 3435673055@qq.com
Time of completion��2022.12.20
Last edited: 2022.12.20
*/
/*
* 固定工资销售
*/
public class BasePlusSalesEmployee extends SalesEmployee {
private double beasSalar;
public BasePlusSalesEmployee(String name, int birthday, double saleNum, double plusRate, double beasSalar) {
super(name, birthday, saleNum, plusRate);
this.beasSalar = beasSalar;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + beasSalar;
}
public double getBeasSalar() {
return beasSalar;
}
public void setBeasSalar(double beasSalar) {
this.beasSalar = beasSalar;
}
}
|