我有一个父类:
public class Partida {
private ArrayList<Jugador> jugadores;
private Tablero tablero;
private ArrayList<PartidaListener> observadores;
public Partida(Tablero tablero, ArrayList<Jugador> jugadores) {
observadores = new ArrayList<>();
this.tablero = tablero;
this.jugadores = jugadores;
}
由以下类继承:
public class PartidaC4 extends Partida {
private ArrayList<JugadorC4> jugadores;
private TableroC4 tablero;
private ArrayList<PartidaListenerC4> observadores;
public PartidaC4(TableroC4 tablero, ArrayList<JugadorC4> jugadores) {
observadores = new ArrayList<>();
this.tablero = tablero;
this.jugadores = jugadores;
}
写这篇文章时,我收到错误
Error:(39, 73) error: constructor Partida in class Partida cannot be applied to given types;
required: Tablero,ArrayList found: no arguments
reason: actual and formal argument lists differ in length
我认为这是非常基本的,但我找不到解决方案。我尝试使用 Android Studio 提供的两种解决方案(使用 super()
),但我对此解决方案不满意,因为我需要使用与父类中的对象类型不同的对象类型。
我应该做什么?
最佳答案
您可以为构造函数定义所需的任何参数,但有必要调用父类(super class)的一个构造函数作为您自己的构造函数的第一行。这可以使用 super() 或 super(arguments) 来完成。
public class PartidaC4 extends Partida {
public PartidaC4() {
super(tablero,jugadores);
//do whatever you want to do in your constructor here
}
public PartidaC4(TableroC4 tablero, ArrayList<JugadorC4> jugadores) {
super(tablero, jugadores);
//do whatever you want to do in your constructor here
}
}
关于java - 没有可用的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49615865/