我的课程有问题。一切都很好,除了公共HourlyEmployee(HourlyEmployee每小时)下超级(每小时)的行。我不知道我是否使用超级错误或如何修复它。它只是说“实际和正式的论点长度不同。不确定这意味着什么。” 谢谢
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;
}
}
答案 0 :(得分:2)
你需要确保有任何构造函数,如
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类中的。如果超类'Employee'没有像上面提到的那样的构造函数。当您尝试编译HourlyEmployee.java时,您将收到消息“实际和正式参数的长度不同”。
这意味着您的超级“员工”没有需要将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