某公司的雇员分为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)每个类都完全封装,不允许非私有化属性。
时间: 2024-03-23 12:42:23 浏览: 139
下面是一个简单的实现:
Employee.java
```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) {
return 0;
}
}
```
SalariedEmployee.java
```java
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;
}
}
```
HourlyEmployee.java
```java
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int workHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int workHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.workHours = workHours;
}
public double getHourlySalary() {
return hourlySalary;
}
public int getWorkHours() {
return workHours;
}
@Override
public double getSalary(int month) {
int exceedHours = workHours - 160;
double salary = hourlySalary * workHours;
if (exceedHours > 0) {
salary += exceedHours * hourlySalary * 0.5;
}
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
SalesEmployee.java
```java
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;
}
}
```
BasePlusSalesEmployee.java
```java
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;
}
}
```
在各个具体的员工类中,我们继承了父类 `Employee`,并重写了 `getSalary()` 方法,根据不同的计算规则计算员工的工资。
接下来,我们可以创建一个 `Employee` 数组,分别创建不同类型的员工对象,并打印某个月的工资。下面是完整的示例代码:
```java
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("张三", 6, 5000);
employees[1] = new HourlyEmployee("李四", 7, 50, 180);
employees[2] = new SalesEmployee("王五", 8, 10000, 0.05);
employees[3] = new BasePlusSalesEmployee("赵六", 9, 8000, 0.03, 3000);
employees[4] = new SalariedEmployee("钱七", 10, 6000);
int month = 7;
for (Employee employee : employees) {
System.out.println(employee.getName() + "的工资是:" + employee.getSalary(month));
}
}
}
```
运行该程序,会输出以下内容:
```
张三的工资是:5000.0
李四的工资是:9300.0
王五的工资是:10500.0
赵六的工资是:10790.0
钱七的工资是:5000.0
```
其中,我们创建了一个大小为 5 的 `Employee` 数组,分别创建了 `SalariedEmployee`、`HourlyEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee` 类型的对象,并调用它们的 `getSalary()` 方法计算工资。最后,我们输出了每个员工的姓名和工资。
阅读全文