java 公司支出的总薪水,要求有一个abstract类,类名为Employee。Employee的子类有YearWork、MonthWork、WeekWork。YearWork对象按年领取薪水, MonthWork按月领取薪水,WeekWork按周领取薪水。Employee类有一个abstract方法:public abstract earnings( ); 子类必须重写父类的earnings( )方法,给出各自领取薪水的具体方法。 另外,有一个Company类,该类用于Employee对象数组作为成员,Employee对象数据的单元可以是YearWork对象的上转型对象、MonthWork对象的上转型对象、WeekWork对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
时间: 2024-03-18 21:39:31 浏览: 201
java职工工资管理系统,包括导入java代码步骤录屏、导入数据库步骤录屏。有源代码和实验报告,代码有详细的注释,直接导入就可
5星 · 资源好评率100%
好的,这是一个关于Java面向对象编程的问题。下面是代码实现:
```
abstract class Employee {
public abstract double earnings();
}
class YearWork extends Employee {
private double salary;
public YearWork(double salary) {
this.salary = salary;
}
public double earnings() {
return salary;
}
}
class MonthWork extends Employee {
private double salary;
public MonthWork(double salary) {
this.salary = salary;
}
public double earnings() {
return salary * 12;
}
}
class WeekWork extends Employee {
private double salary;
public WeekWork(double salary) {
this.salary = salary;
}
public double earnings() {
return salary * 52;
}
}
class Company {
private Employee[] employees;
public Company(Employee[] employees) {
this.employees = employees;
}
public double totalEarnings() {
double total = 0;
for (Employee e : employees) {
total += e.earnings();
}
return total;
}
}
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = new YearWork(50000);
employees[1] = new MonthWork(4000);
employees[2] = new WeekWork(1000);
Company company = new Company(employees);
System.out.println("The total earnings of the company in one year is " + company.totalEarnings());
}
}
```
在这里,我们先定义了一个抽象类Employee,其中有一个抽象方法earnings(),表示每个员工的具体领薪方式。然后我们分别定义了三个子类:YearWork、MonthWork、WeekWork,表示年、月、周领取薪水的员工。这些子类需要实现earnings()方法,给出各自领取薪水的具体方法。
最后,我们定义了一个Company类,用于存储Employee对象数组,并通过totalEarnings()方法计算公司一年需要支付的薪水总额。在main()方法中,我们创建了一个Employee对象数组,并将其分别初始化为三个子类的对象,然后创建一个Company对象并将Employee对象数组传递给它,最后输出公司一年需要支付的薪水总额。
希望这个例子能够帮助你理解Java面向对象编程。
阅读全文