五、编程题 某公司的雇员分为以下若干类: (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会 额外奖励100 元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ① 属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加 上销售提成部分。 ① 属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打 印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2024-03-05 08:49:24 浏览: 203
好的,我可以帮你解答这道编程题。你可以参考以下代码实现:
```java
abstract class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public abstract double getSalary(int month);
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
}
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
@Override
public double getSalary(int month) {
double salary = monthlySalary;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
class HourlyEmployee extends Employee {
private double hourlyWage;
private double monthlyHours;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, double monthlyHours) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.monthlyHours = monthlyHours;
}
@Override
public double getSalary(int month) {
double salary = hourlyWage * monthlyHours;
if (monthlyHours > 160) {
salary += hourlyWage * 0.5 * (monthlyHours - 160);
}
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
super(name, birthMonth);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) {
super(name, birthMonth, monthlySales, commissionRate);
this.baseSalary = baseSalary;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (getBirthMonth() == month) {
salary += 100;
}
return salary;
}
}
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("Tom", 3, 5000);
employees[1] = new HourlyEmployee("Jim", 7, 50, 180);
employees[2] = new SalesEmployee("Lucy", 9, 100000, 0.1);
employees[3] = new BasePlusSalesEmployee("Lily", 12, 80000, 0.08, 5000);
employees[4] = new HourlyEmployee("Jack", 5, 60, 140);
int month = 9;
for (Employee employee : employees) {
System.out.println(employee.getName() + "'s salary in month " + month + " is " + employee.getSalary(month));
}
}
}
```
在这个程序中,我们定义了一个抽象类 `Employee`,包含员工的基本属性和方法。然后,我们创建了四个子类来继承 `Employee`,分别是 `SalariedEmployee`、`HourlyEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee`。每个子类中都重写了 `getSalary` 方法,以根据不同的工资计算方式来计算员工的工资。
在 `Main` 类中,我们创建了一个 `Employee` 数组,并分别创建了不同类型的员工对象。然后,我们遍历数组,调用 `getSalary` 方法来计算每个员工在给定月份的工资,并输出结果。
希望这个答案能够帮助到你!
阅读全文