Java某公司的雇员分为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-02-11 11:04:52 浏览: 185
好的,根据您的要求,我来给您完成这个任务。
首先,我们需要定义 Employee 类,代码如下:
```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
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
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 += (hoursWorked - 160) * 1.5 * hourlyWage;
}
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
}
```
然后,我们定义 SalesEmployee 类,代码如下:
```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
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;
return salary;
}
}
```
现在我们可以在主函数中创建 Employee 数组并打印某个月的工资了,代码如下:
```java
public static void main(String[] args) {
Employee[] employees = new Employee[5];
employees[0] = new SalariedEmployee("张三", 1, 5000);
employees[1] = new HourlyEmployee("李四", 2, 20, 180);
employees[2] = new SalesEmployee("王五", 3, 10000, 0.1);
employees[3] = new BasePlusSalesEmployee("赵六", 4, 20000, 0.08, 5000);
employees[4] = new SalariedEmployee("小明", 5, 6000);
int month = 5; // 打印五月份的工资
for (Employee employee : employees) {
System.out.println(employee.getName() + "的工资为:" + employee.getSalary(month));
}
}
```
以上就是完成您的要求的代码,您可以尝试运行一下看看结果。
阅读全文