Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 继承导致构造函数出错?_Java_Inheritance_Constructor - Fatal编程技术网

Java 继承导致构造函数出错?

Java 继承导致构造函数出错?,java,inheritance,constructor,Java,Inheritance,Constructor,我有一个名为CreatureAi的类,代码如下 public class CreatureAi { public Creature creature; public CreatureAi(Creature creature) { this.creature = creature; this.creature.setCreatureAi(this); } // There's more, I'm shortening it. 我有

我有一个名为CreatureAi的类,代码如下

public class CreatureAi {
    public Creature creature;

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
我有一个名为PlayerAi的类,它扩展了它

public class PlayerAi extends CreatureAi {
    private FieldOfView fov;
    private Player player;

    public PlayerAi(Player player, FieldOfView fov) {
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
    }
    // These are the only constructors.
然而,Netbeans给了我这个错误

constructer CreatureAi in class CreatureAi cannot be applied to the given types.
required: Creature
found: No arguements
reason: Actual and formal lists differ in length.

为什么会出现此错误?

当您编写子类时,隐式调用super()中的super类型构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
正如您在代码中所显示的,您的基类没有
无arg
构造函数。所以你的孩子是无效的。您必须调用一个有效的超级构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
或者,如果您可以修改
创建者ai
,则可以添加默认的无参数构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.

编写子类时,隐式调用超级类型构造函数中的
super()

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
正如您在代码中所显示的,您的基类没有
无arg
构造函数。所以你的孩子是无效的。您必须调用一个有效的超级构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
或者,如果您可以修改
创建者ai
,则可以添加默认的无参数构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.

编写子类时,隐式调用超级类型构造函数中的
super()

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
正如您在代码中所显示的,您的基类没有
无arg
构造函数。所以你的孩子是无效的。您必须调用一个有效的超级构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
或者,如果您可以修改
创建者ai
,则可以添加默认的无参数构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.

编写子类时,隐式调用超级类型构造函数中的
super()

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
正如您在代码中所显示的,您的基类没有
无arg
构造函数。所以你的孩子是无效的。您必须调用一个有效的超级构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
或者,如果您可以修改
创建者ai
,则可以添加默认的无参数构造函数

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}
public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }
public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.


super(生物)
在第一行中,您没有在中没有参数的构造函数CreatureAi@nachokk我在哪里写的超级(生物)?检查关于对象构造的Java语言参考:
super(生物)
在第一行,你没有没有没有参数的构造函数CreatureAi@nachokk我在哪里写的超级(生物)?检查有关对象构造的Java语言参考:
super(bioture)
在第一行中,您没有在中没有参数的构造函数CreatureAi@nachokk我在哪里写的超级(生物)?查看Java语言中关于对象构造的参考:
super(生物)
,在第一行,您没有在中没有参数的构造函数CreatureAi@nachokk我在哪里写的超级(生物)?检查关于对象构造的Java语言参考:所以我的子类自动调用我的父构造函数?基本上,你需要将
super(aPlayer,aFov)
作为
PlayerAi
构造函数中的第一条指令,或者为
CreatureAi
创建一个无参数构造函数。是的,每次初始化中的类时,都会从
Object
到实际类依次调用所有父构造函数,通过所有的类层次结构。@ AuBiNoNoBB您可能要考虑的是编写<代码> Ai < /代码>接口,即<代码> CreatureAi < /代码>和<代码> PraveAi实现,而不是试图使<代码> PraveAi继承自<代码> CreatureAi < /代码>。我发现我在实践中很少使用继承,而且在我看来,我的代码更干净,因为它。对于新手来说,在创建对象后,从构造函数中删除parm并使用setter设置值可能是最简单的。不太“优雅”,但更容易理解。所以我的子类会自动调用我的父构造函数?基本上,你需要将
super(aPlayer,aFov)
作为
PlayerAi
构造函数中的第一条指令,或者为
CreatureAi
创建一个无参数构造函数。是的,每次初始化一个类,按照从
对象到实际类的顺序调用所有父构造函数,通过所有的类层次结构。@ AuBiNoNoBB您可能要考虑的是编写<代码> Ai < /代码>接口,即<代码> CreatureAi < /代码>和<代码> PraveAi实现,而不是试图使<代码> PraveAi继承自<代码> CreatureAi < /代码>。我发现我在实践中很少使用继承,而且在我看来,我的代码更干净,因为它。对于新手来说,在创建对象后,从构造函数中删除parm并使用setter设置值可能是最简单的。不太“优雅”,但更容易理解。所以我的子类会自动调用我的父构造函数?基本上,你需要将
super(aPlayer,aFov)
作为
PlayerAi
构造函数中的第一条指令,或者为
CreatureAi
创建一个无参数构造函数。是的,每次初始化一个类,按照从
对象到实际类的顺序调用所有父构造函数,通过所有的类层次结构。@ AuBiNoNoBB您可能要考虑的是编写<代码> Ai < /代码>接口,即<代码> CreatureAi < /代码>和<代码> PraveAi实现,而不是试图使<代码> PraveAi继承自<代码> CreatureAi < /代码>。我发现我在实践中很少使用继承,而且在我看来,我的代码更干净,因为它。对于新手来说,在创建对象后,从构造函数中删除parm并使用setter设置值可能是最简单的。不太“优雅”,但更容易理解。所以我的子类会自动调用我的父构造函数?基本上,你需要将
super(aPlayer,aFov)
作为
PlayerAi
构造函数中的第一条指令,或者为
CreatureAi
创建一个无参数构造函数。是的,每次初始化一个类,所有的父构造函数都是从“代码>对象/代码>到实际的类,通过所有的类层次结构来调用的。@ AuBuiNoBOB你想考虑的一件事是制作一个<代码> Ai < /代码>接口,这两个接口都是<代码> CreatureAi < /代码>和<代码> PraveAi<代码>实现。