编程题 某公司的雇员分为5类,每类员工都有相应的封装类,这5个类的信息如下所示。 (1) Employee: 这是所有员工总的父类。 ①属性:员工的姓名,员工的生日月份 ②方法:getSalary (int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖历100 元。 ② SalariedEmployee: Employee 的子类,拿固定工资的员工。 ①属性:月薪。 (3)HourlyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。 ①属性:每小时的工资、每月工作的小时数。 (④) SalesEmployee: Employee 的子类,销售,王资由月销售额和提成率诀定。 ①属性:月销售额、提成率。 (5) BasePlus SalesEmployee: SalesEmployee 的子类,有固定底新的销售人员,工资由底新加 上销售提成部分。 ①属性:底薪。 本题要求根据上述雇员分类,编写一个程序,实现以下功能: (1)创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。 (2)每个类都完全封装,不允许非私有化属性。
时间: 2024-02-25 19:58:21 浏览: 34
以下是实现上述功能的代码示例:
```java
public class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public String getName() {
return name;
}
public int getBirthMonth() {
return birthMonth;
}
public double getSalary(int month) {
double salary = 0;
// 具体实现根据子类的不同而不同,此处略去
return salary;
}
}
public class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
@Override
public double getSalary(int month) {
double salary = monthlySalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class HourlyEmployee extends Employee {
private double hourlyWage;
private double hoursWorked;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, double hoursWorked) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.hoursWorked = hoursWorked;
}
public double getHourlyWage() {
return hourlyWage;
}
public double getHoursWorked() {
return hoursWorked;
}
@Override
public double getSalary(int month) {
double salary = hourlyWage * hoursWorked;
if (hoursWorked > 160) {
salary += hourlyWage * 1.5 * (hoursWorked - 160);
}
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public 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;
}
public double getMonthlySales() {
return monthlySales;
}
public double getCommissionRate() {
return commissionRate;
}
@Override
public double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public 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;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("John", 3, 5000);
employees[1] = new HourlyEmployee("Mary", 4, 20, 180);
employees[2] = new SalesEmployee("Tom", 5, 10000, 0.1);
employees[3] = new BasePlusSalesEmployee("Jerry", 6, 8000, 0.12, 3000);
employees[4] = new HourlyEmployee("Lucy", 7, 25, 150);
int month = 5;
for (Employee employee : employees) {
System.out.println(employee.getName() + " " + employee.getSalary(month));
}
}
}
```
在 `Test` 类中,我们创建了一个 `Employee` 数组,分别创建了不同类型的 `Employee` 对象,并打印了 5 月份的工资。注意到我们在 `Employee` 类中实现了 `getSalary` 方法,而在每个子类中都重写了该方法,实现了不同类型员工的不同计算规则。同时,通过使用 `private` 修饰成员变量,实现了完全封装。