java - 隐式 super 构造函数。必须显式调用另一个构造函数

标签 java inheritance implicit explicit

我刚刚开始在我的类中使用继承,这是我遇到的第一个错误。除了在错误部分抛出标题的构造函数之外,大多数代码都可以正常工作。

public class CellDoor extends CellPassage {

    // TODO: instance variable(s)!
    private String imageOpen,imageClosed;
    private boolean locked, occupied;
    private Item item;

    // Create a new cell in the dungeon with the specified image.
    // The CellDoor class represents an door that can be closed (locked) or open.
    // NOTE: an open door is allowed to hold an item (e.g. a gem or key).
    public CellDoor(String imageOpen, String imageClosed, boolean locked) {
        this.imageOpen = imageOpen;
        this.imageClosed = imageClosed;
        this.locked = locked;
    }

cellPassage 构造函数是:

public CellPassage(String image) {
    this.image = image;
}

有人可以给我一些建议吗?

最佳答案

您可能在 CellPassage 类中有一个不是默认构造函数。这意味着 Java 无法通过调用默认的 super 构造函数来创建您的 CellDoor 对象。您必须在构造函数主体的第一行添加一个 super(...) ,其中 ... 是 CellPassage 类中构造函数的参数。

public CellDoor(String imageOpen, String imageClosed, boolean locked)
{
    super(imageOpen);
    this.imageOpen = imageOpen;
    this.imageClosed = imageClosed;
    this.locked = locked;
}

如果您提供 CellPassage 类中的代码,我们将轻松确定您应该如何编写 CellDoor 构造函数

关于java - 隐式 super 构造函数。必须显式调用另一个构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610980/

相关文章:

java - 如何使用 apache poi 4.0 XDDFChartData、XDDFPieChartData 生成饼图;在使用 java 的 excel 表中,我不想使用 jfree

java - 如何在执行时设置文件变量名

inheritance - 如何避免 Entity Framework TPT 继承中的多态行为以有效查询基类型

scala - 隐式的范围

c# - 同时实现隐式和显式接口(interface)是否有意义?

java - gwt - 为什么有两个 "Element"类型?

java - GraphStream - 使用 netstream 的问题

java - 使用包含数据的新 Array 或 ArrayList 调用父类(super class)的构造函数?

java - Hibernate 级联继承

scala - 所有子类型的隐式处理器