Java编译错误求助:Event类构造方法参数不匹配问题
编译错误原因分析与解决方法
这个编译错误的核心原因很清晰:你的ArrivalEvent子类在初始化时,没有正确调用父类Event的构造函数。
为什么会报错?
在Java的继承规则里,子类的构造函数默认会自动调用父类的无参构造函数。但如果你的Event类没有定义无参构造(比如它只提供了带参数的构造函数),编译器找不到可以自动调用的无参构造,就会抛出constructor Event in class Event cannot be applied to given types这个错误——说白了就是编译器不知道怎么帮你初始化父类部分的内容。
看你的ArrivalEvent代码,构造函数里完全没有调用super(...)来触发父类的构造,这就是问题所在。
解决步骤
- 先查看你的
Event类的构造函数签名,确认它提供了哪些构造方式(比如是带Customer参数?还是带时间参数?或者其他组合)。 - 在
ArrivalEvent的构造函数第一行,显式调用父类的构造函数,使用super()关键字并传入对应的参数。
举个具体的例子:假设你的Event类的构造函数是这样的:
public class Event { private Customer customer; public Event(Customer customer) { this.customer = customer; } }
那修改后的ArrivalEvent代码应该是:
class ArrivalEvent extends Event { Counter counterList[]; Customer currentCustomer; Counter currentCounter; public ArrivalEvent(Counter[] counterList, Customer currentCustomer){ super(currentCustomer); // 必须放在构造函数的第一行,显式调用父类构造 this.counterList = counterList; this.currentCustomer = currentCustomer; } }
如果你的Event构造需要其他参数(比如事件发生的时间),只需要把super()里的参数换成对应的值即可——关键是要和父类构造函数的参数列表完全匹配。
内容的提问来源于stack exchange,提问作者user13639182
相关产品推荐
相关产品推荐

