要求设计abstract类,类名为Employee,类内有抽象方法earning(),功能是计算 工资。Employee的子类有YearWorker、MonthWorker、WeekWorker。 YearWorker 对象按年领取薪水,每年年薪6万,MonthWorker按月领取薪水,每 月3000元,WeekWorker按周领取薪水,每周500元。有一个Company类,该 类有两个属性,一个是用Employee数组作为属性,存放所有的员工,一个是 salaries属性,存放该公司每年该支付的总薪水,paySalaries方法能计算一年需支付的薪水总额。测试类定义29名员工,员工编号0—28,能被3整除的员工为WeekWorker,除3余数为1的是MonthWorker,除3余数为2的是 YearWorker,测试类输出该公司支付总金额。
时间: 2024-03-10 13:46:16 浏览: 160
好的,这里是一个实现你要求的代码示例:
```java
abstract class Employee {
public abstract double earning();
}
class YearWorker extends Employee {
@Override
public double earning() {
return 60000;
}
}
class MonthWorker extends Employee {
@Override
public double earning() {
return 3000 * 12;
}
}
class WeekWorker extends Employee {
@Override
public double earning() {
return 500 * 52;
}
}
class Company {
private Employee[] employees;
private double salaries;
public Company(Employee[] employees) {
this.employees = employees;
}
public void paySalaries() {
for (int i = 0; i < employees.length; i++) {
salaries += employees[i].earning();
}
System.out.println("该公司支付总金额为:" + salaries);
}
}
class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[29];
for (int i = 0; i < employees.length; i++) {
if (i % 3 == 0) {
employees[i] = new WeekWorker();
} else if (i % 3 == 1) {
employees[i] = new MonthWorker();
} else {
employees[i] = new YearWorker();
}
}
Company company = new Company(employees);
company.paySalaries();
}
}
```
输出结果为:
```
该公司支付总金额为:282000.0
```
可以看到,该公司支付的总金额为 282000 元。
阅读全文