gpt4 book ai didi

java - java中使用super(),小时工方法

转载 作者:行者123 更新时间:2023-12-02 02:19:29 24 4
gpt4 key购买 nike

我的一门类(class)遇到问题。除了 public HourlyEmployee(HourlyEmployee hourly) 下的 super(hourly) 之外,一切都很好。我不知道我是否使用了 super 错误或如何修复它。它只是说“实际和正式的论点长度不同。不确定这意味着什么。”谢谢

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;
}

}

最佳答案

您需要确保 do There 有任何构造函数,例如

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 类中。如果父类(super class)“Employee”没有像上面提到的两个那样的构造函数。当您尝试编译 HourlyEmployee.java 时,您将收到消息“实际参数和形式参数长度不同”

这意味着您的父类(super class)“Employee”没有需要传递 HourlyEmployee 或其父类(super class)实例的构造函数。

事实上,您需要显示有关编译器错误消息的更多信息。我猜您得到了这样的信息。

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

关于java - java中使用super(),小时工方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48723508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com