java - 没有可用的默认构造函数

标签 java android oop

我有一个父类:

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/

相关文章:

Java:如何在运行 JAR 文件时获取文件路径

java - 非方形物体的碰撞检测

android - React native 0.61.5 Crashlytics 缺少 libhermes.so : SoLoader. java com.facebook.soloader.SoLoader.assertInitialized

android - 如何轻松处理方向变化

java - 从派生类调用构造函数

java - Selenium:获取 chrome 未正确关闭

java - 从 Spring Boot 应用程序连接到 Oracle 数据库时如何修复 "Driver does not support get/set network timeout for connections"?

android - 将 AIR Android p12 keystore 文件迁移到 Cordova

python - 如何在Python中使用变量的值作为类型提示

Java:当其他类已经扩展基类时如何扩展基类?