我的一门课遇到问题。一切都很好,除了在公共HourlyEmployee(HourlyEmployee hourly)下具有super(hourly)的行之外。我不知道我是否使用超级错误或如何修复它。它只是说“实际和形式上的论点的长度不同。不确定那是什么意思。”
谢谢

package payrollsystem_1;
import java.util.ArrayList;

public class HourlyEmployee extends Employee {
    private double hourlyRate;
    private double periodHours;

    public HourlyEmployee(int employeeID, String firstName, String lastName,
            ArrayList<Paycheck> listOfPaychecks, double hourlyRate, double periodHours ){

        super(employeeID, firstName, lastName, listOfPaychecks);
        this.listOfPaychecks = listOfPaychecks;
        this.hourlyRate = hourlyRate;
        this.periodHours = periodHours;
    }

    public HourlyEmployee(HourlyEmployee hourly) {
       super(hourly);
        this.hourlyRate = hourly.hourlyRate;
        this.periodHours = hourly.periodHours;
    }

    public double getHourlyRate(){
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public double getPeriodHours() {
        return periodHours;
    }

    public void setPeriodHours(double periodHours) {
        this.periodHours = periodHours;
    }

}

最佳答案

您需要确保那里有任何类似的构造函数

public Employee(HourlyEmployee hourly) {
  //I know the super class shouldn't know about the subclass.
  //But this is OK if you write like this.
  //It can be compiled without showing any errors.
  /*code*/
}


要么

public Employee(Employee hourly) {
  /*code*/
}


如果您的超类'Employee'没有像上面提到的两个构造函数。当您尝试编译HourlyEmployee.java时,将收到消息“实际参数和形式参数的长度不同”。

这意味着您的超类'Employee'没有需要将HourlyEmployee或其超类实例传递给的构造函数。

实际上,您需要显示有关编译器错误消息的更多信息。我想您已经是这样了。

HourlyEmployee.java:xx: error: constructor Employee in class Entity cannot be applied to the given types:
public Employee(int employeeID, String firstName, String lastName, ArrayList<Object> listOfPaychecks)
    required: int,String,String,ArrayList<Paycheck>
    found: HourlyEmployee
    reason: actual and formal argument lists differ in length

10-05 22:43