某公司的雇员分为以下若干类: (1) Employee: 这是所有员工总的父类 属性:员工的姓名,员工的生日月份 @方法: getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元. (2) SalariedEmployee: Employee 的子类,拿固定工资的员工属性: 月薪。 (3)HoulyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee: Employee 的子类,销售,工资由月销售额和提成率决定属性:月销售额、提成率。(5) BasePlusSalesEmployee: SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 属性: 底薪。 要求: 创建一个Employee 数组,分别创建若千不同的Eployee对象,并打印某个月的工资。1 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2024-03-23 09:42:14 浏览: 181
好的,您的问题是如何创建Employee数组并打印某个月的工资。
首先,我们需要定义Employee及其子类的属性和方法,并且将它们封装起来。以下是一个可能的实现:
```java
public abstract 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 abstract double getSalary(int month);
}
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 hourlySalary;
private int monthlyWorkHours;
public HourlyEmployee(String name, int birthMonth, double hourlySalary, int monthlyWorkHours) {
super(name, birthMonth);
this.hourlySalary = hourlySalary;
this.monthlyWorkHours = monthlyWorkHours;
}
public double getHourlySalary() {
return hourlySalary;
}
public int getMonthlyWorkHours() {
return monthlyWorkHours;
}
@Override
public double getSalary(int month) {
double salary = hourlySalary * monthlyWorkHours;
if (monthlyWorkHours > 160) {
salary += hourlySalary * 1.5 * (monthlyWorkHours - 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;
}
}
```
接下来,我们可以创建一个Employee数组,将各种类型的员工对象存储在数组中,然后打印出每个员工在某个月的工资:
```java
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[] {
new SalariedEmployee("Alice", 1, 5000),
new HourlyEmployee("Bob", 2, 20, 180),
new SalesEmployee("Charlie", 3, 10000, 0.1),
new BasePlusSalesEmployee("David", 4, 8000, 0.15, 2000)
};
int month = 5;
for (Employee employee : employees) {
System.out.println(employee.getName() + "'s salary in month " + month + " is " + employee.getSalary(month));
}
}
}
```
上述代码中,我们创建了一个包含四种不同类型员工的Employee数组,并且打印了每个员工在第5个月的工资。
阅读全文