要为某个公司编写雇员( n个雇员)支付月工资,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资利用Java写
时间: 2024-04-08 17:12:23 浏览: 121
可以定义一个抽象类Employee作为所有雇员类型的基类,然后派生出具体的子类,如HourlyEmployee、SalariedEmployee、CommissionEmployee等。
抽象类Employee中定义了基本的属性和方法,如姓名、工号、基本工资、计算工资的方法等,具体的子类可以根据自己的特点对这些属性和方法进行扩展和重写。
下面是一个简单的实现示例:
```java
abstract class Employee {
private String name; // 姓名
private String id; // 工号
private double baseSalary; // 基本工资
public Employee(String name, String id, double baseSalary) {
this.name = name;
this.id = id;
this.baseSalary = baseSalary;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getBaseSalary() {
return baseSalary;
}
// 计算工资的抽象方法
public abstract double getSalary();
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", baseSalary=" + baseSalary +
'}';
}
}
class HourlyEmployee extends Employee {
private int hours; // 工作小时数
private double hourlyRate; // 每小时工资
public HourlyEmployee(String name, String id, double baseSalary, int hours, double hourlyRate) {
super(name, id, baseSalary);
this.hours = hours;
this.hourlyRate = hourlyRate;
}
public int getHours() {
return hours;
}
public double getHourlyRate() {
return hourlyRate;
}
@Override
public double getSalary() {
return getBaseSalary() + hours * hourlyRate;
}
@Override
public String toString() {
return "HourlyEmployee{" +
"name='" + getName() + '\'' +
", id='" + getId() + '\'' +
", baseSalary=" + getBaseSalary() +
", hours=" + hours +
", hourlyRate=" + hourlyRate +
", salary=" + getSalary() +
'}';
}
}
class SalariedEmployee extends Employee {
public SalariedEmployee(String name, String id, double baseSalary) {
super(name, id, baseSalary);
}
@Override
public double getSalary() {
return getBaseSalary();
}
@Override
public String toString() {
return "SalariedEmployee{" +
"name='" + getName() + '\'' +
", id='" + getId() + '\'' +
", baseSalary=" + getBaseSalary() +
", salary=" + getSalary() +
'}';
}
}
class CommissionEmployee extends Employee {
private double sales; // 销售额
private double commissionRate; // 提成比例
public CommissionEmployee(String name, String id, double baseSalary, double sales, double commissionRate) {
super(name, id, baseSalary);
this.sales = sales;
this.commissionRate = commissionRate;
}
public double getSales() {
return sales;
}
public double getCommissionRate() {
return commissionRate;
}
@Override
public double getSalary() {
return getBaseSalary() + sales * commissionRate;
}
@Override
public String toString() {
return "CommissionEmployee{" +
"name='" + getName() + '\'' +
", id='" + getId() + '\'' +
", baseSalary=" + getBaseSalary() +
", sales=" + sales +
", commissionRate=" + commissionRate +
", salary=" + getSalary() +
'}';
}
}
public class PayrollSystem {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = new HourlyEmployee("张三", "1001", 2000, 160, 12.5);
employees[1] = new SalariedEmployee("李四", "1002", 5000);
employees[2] = new CommissionEmployee("王五", "1003", 3000, 20000, 0.05);
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
```
输出结果:
```
HourlyEmployee{name='张三', id='1001', baseSalary=2000.0, hours=160, hourlyRate=12.5, salary=4000.0}
SalariedEmployee{name='李四', id='1002', baseSalary=5000.0, salary=5000.0}
CommissionEmployee{name='王五', id='1003', baseSalary=3000.0, sales=20000.0, commissionRate=0.05, salary=4000.0}
```
在PayrollSystem类中创建了一个Employee类型的数组,包含了三种不同类型的雇员,然后依次输出每个雇员的信息,包括姓名、工号、基本工资、实际工资等。每个具体的雇员类型都实现了抽象类Employee中定义的计算工资的抽象方法,根据自己的特点进行计算。
阅读全文