某公司的雇员分为3类,每类员工都有相应的封装类,类的信息如下所示。(1) Employee:这是所有员工总的父类。① 属性:员工的姓名,员工的生日月份② 方法:getSalary(int month) ,参数为月份,如果该月是员工的生日,则公司会额外奖励100 元,否则默认返回0。(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。① 属性:月薪。(3) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。① 属性:月销售额、提成率。(4) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。① 属性:底薪。(5)另创建一个Company类,该类有一个属性为Employee数组,用于存储公司的员工对象。有一个displaySalay(int month),输出:xxx(姓名)x月的工资是:xxx.xx元(保留两位小数)本题要求根据上述雇员分类,编写一个程序,满足一下要求:(1)不允许有public和默认属性,根据需求,使用protected和private修饰属性;(2)每个类必须有一个全参数的构造函数;(3)合理使用this和super关键字访问属性、方法和构造函数;(4)在main方法里创建若干个Employee的子类的对象,并将这些存入一个Employee 数组;(5)以步骤(4)中的Employee数组为参数,创建一个Company的对象,调用该对象的displaySalay(int month)方法输出所有员工某月的工资。
时间: 2024-02-13 07:59:53 浏览: 80
好的,我理解了你的问题。下面是代码实现:
```java
import java.text.DecimalFormat;
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); //计算工资的抽象方法
}
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(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;
}
public double getMonthlySales() {
return monthlySales;
}
public double getCommissionRate() {
return 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;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double getSalary(int month) {
double salary = super.getSalary(month) + baseSalary;
return salary;
}
}
class Company {
private Employee[] employees;
public Company(Employee[] employees) {
this.employees = employees;
}
public void displaySalary(int month) {
DecimalFormat df = new DecimalFormat("0.00");
for(Employee employee : employees) {
System.out.println(employee.getName() + " " + month + "月的工资是:" + df.format(employee.getSalary(month)) + "元");
}
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[4];
employees[0] = new SalariedEmployee("张三", 8, 5000);
employees[1] = new SalesEmployee("李四", 6, 10000, 0.1);
employees[2] = new BasePlusSalesEmployee("王五", 5, 20000, 0.05, 3000);
employees[3] = new SalariedEmployee("赵六", 11, 6000);
Company company = new Company(employees);
company.displaySalary(6);
}
}
```
代码中,我们首先定义了一个抽象类 `Employee`,包含员工的姓名和生日月份属性以及计算工资的抽象方法 `getSalary`。然后我们定义了三个员工类 `SalariedEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee`,它们都继承自 `Employee`。`SalariedEmployee` 表示拿固定工资的员工,有月薪属性;`SalesEmployee` 表示销售,有月销售额和提成率属性;`BasePlusSalesEmployee` 表示有固定底薪的销售人员,有底薪属性。这三个类都实现了 `getSalary` 方法,根据具体情况计算员工的工资。
最后,我们创建了一个 `Company` 类,包含一个 `Employee` 数组,用于存储公司的员工对象。该类有一个 `displaySalary` 方法,遍历数组中的每个员工对象,调用 `getSalary` 方法计算该员工在指定月份的工资,输出格式为“姓名 月份的工资是:xxx.xx元”。
在 `main` 方法中,我们创建了四个员工对象并存入一个 `Employee` 数组中,然后创建了一个 `Company` 对象,调用其 `displaySalary` 方法输出所有员工在6月份的工资。
阅读全文