Java:当我实例化抽象类的子类时,它无法识别其超类的构造函数

发布于 2024-11-04 10:36:37 字数 1129 浏览 3 评论 0原文

我没有太多的 Java 经验,但我看到代码中有一个带有特定构造函数的抽象类,然后是该抽象类的一个没有构造函数的子类。然后,当子类实例化时,它是用其超类构造函数构造的。是这样吗?

我有这个抽象类:

public abstract class Tile{

    public int x;
    public int y;
    public int z;

    protected Color color;
    protected float friction;
    protected float bounce;
    protected boolean liquid;

    public void Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
        init();
    }
    abstract protected void init();

和这个子类:

public class TestTile extends Tile{
    protected void init(){
        color = Color.RED;
        friction = 0.1f;
        bounce = 0.2f;
        liquid = false;
    }
}

但是当我用这个实例化 TestTile 时:

Tile tile = new TestTile(0, 0, 0);

init() 方法永远不会运行。其中定义的所有值均为空。我尝试在子类中创建一个我认为可能是冗余的构造函数,它只是使用完全相同的参数调用 super ,但是当我这样做时,即使 super(x, y, z) 是其中唯一的语句,它也是这样说的:

TestTile.java:27:对 super 的调用必须是构造函数中的第一个语句

我想创建一堆实现 Tile 属性的 Tile 子类。如果这不是正确的方法,那么更好的方法是什么?

如果有什么关系的话,我正在使用 32 位 Ubuntu Linux 11.04。

谢谢。

I do not have very much Java experience but I see codes where there is an abstract class with a certain constructor and then a subclass of that abstract class without a constructor. Then when the subclass is instantiated it is constructed with its superclass constructor. Is that right?

I have this abstract class:

public abstract class Tile{

    public int x;
    public int y;
    public int z;

    protected Color color;
    protected float friction;
    protected float bounce;
    protected boolean liquid;

    public void Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
        init();
    }
    abstract protected void init();

And this subclass:

public class TestTile extends Tile{
    protected void init(){
        color = Color.RED;
        friction = 0.1f;
        bounce = 0.2f;
        liquid = false;
    }
}

But when I instantiate a TestTile with this:

Tile tile = new TestTile(0, 0, 0);

the init() method never runs. All of the values defined inside it are null. I tried making what I though might be a redundant constructor in the subclass which just called super with the exact same parameters, but when I did that, even with super(x, y, z) the only statement inside it, it said this:

TestTile.java:27: call to super must be first statement in constructor

I want to make a bunch of subclasses of Tile which implement the properties of a Tile. If this is not the correct way to do that, what is a better way?

I am using 32-bit Ubuntu Linux 11.04 if it has to do with anything.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绻影浮沉 2024-11-11 10:36:37

您的构造函数不是属性构造函数格式,它是 void,请使其:

public Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
        init();
    }

Your constructor is not in propert constructor format, it's void, make it:

public Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
        init();
    }
怪我闹别瞎闹 2024-11-11 10:36:37

我没有看到带有三个参数的 TestTime 构造函数。我根本没有看到任何 ctor,这意味着您所拥有的只是编译器为您提供的默认值。是不是我走得太快而错过了?

我建议仔细注意这一点。我会重新考虑这个设计:

构造函数中的可重写方法调用有什么问题? 尝试这个

- 它包括对构造函数的修复并避免了其他线程指出的问题:

public abstract class Tile{

    public int x;
    public int y;
    public int z;

    protected Color color;
    protected float friction;
    protected float bounce;
    protected boolean liquid;

    public Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}


public class TestTile extends Tile{

    // You're missing this.
    public TestTile(int x, int y, int z)
    {
        super(x, y, z);
        this.init();
    }

    protected void init(){
        color = Color.RED;
        friction = 0.1f;
        bounce = 0.2f;
        liquid = false;
    }
}

I don't see a constructor for TestTime that takes three arguments. I don't see any ctor at all, which means that all you have is the default that the compiler gives you. Did I go too fast and miss it?

I'd recommend paying careful attention to this. I'd rethink this design:

What's wrong with overridable method calls in constructors?

Try this - it includes the fix for your constructor and avoids the issue that the other thread points out:

public abstract class Tile{

    public int x;
    public int y;
    public int z;

    protected Color color;
    protected float friction;
    protected float bounce;
    protected boolean liquid;

    public Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}


public class TestTile extends Tile{

    // You're missing this.
    public TestTile(int x, int y, int z)
    {
        super(x, y, z);
        this.init();
    }

    protected void init(){
        color = Color.RED;
        friction = 0.1f;
        bounce = 0.2f;
        liquid = false;
    }
}
软的没边 2024-11-11 10:36:37

首先,Tile只有一个带x、y、z参数的构造函数,没有默认构造函数,所以必须在TestTile中调用super(x, y, z)代码>构造函数。作为 slandau 说,“构造函数”有一个错误的 void 返回类型。

TestTile 需要声明参数或传递默认值:

public TestTile(int x, int y, int z) {
  super(x, y, z);
}

public TestTile() {
  super(0, 0, 0);
}

在 Java 中,在构造函数中调用抽象方法有很多 risc,另请参阅 此处,实例未正确初始化。您只能安全地调用静态方法(这在这里不起作用)。

public TestTile(int x, int y, int z) {
  super(x, y, z);
  color = Color.RED;
  friction = 0.1f;
  bounce = 0.2f;
  liquid = false;
}

或者您需要在派生类中调用私有方法(从 Tile 中删除抽象 init()):

public TestTile(int x, int y, int z) {
  super(x, y, z);
  init();
}

private void init() {
  color = Color.RED;
  friction = 0.1f;
  bounce = 0.2f;
  liquid = false;
}

您确定成员是这里的正确实现吗?也许抽象方法(getter)在这里可能更好地声明一个行为并在子类中实现它?

public abstract class Tile {
  public int x;
  public int y;
  public int z;

  public Tile(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  public abstract Color getColor();
  public abstract float getFriction();
  public abstract float getBounce();
  public abstract boolean isLiquid();
}

public class TestTile extends Tile {

  public TestTile(int x, int y, int z) {
    super(x, y, z);
  }

  public Color getColor() {
    return Color.RED;
  }

  public float getFriction() {
    return 0.1f;
  }

  public float getBounce() {
    return 0.2f;
  }

  public boolean isLiquid() {
    return false;
  }

}

First of all, Tile has only one constructor with the x, y, z parameters, no default constructor, so you have to call super(x, y, z) in the TestTile constructor. As slandau said, the "constructor" has a wrong void return type.

The TestTile needs to declare the parameters or pass default values:

public TestTile(int x, int y, int z) {
  super(x, y, z);
}

public TestTile() {
  super(0, 0, 0);
}

In Java, there are many riscs to call an abstract method in a constructor, see also here, the instance is not initialized properly. You can only call static methods safe (which will not work here).

public TestTile(int x, int y, int z) {
  super(x, y, z);
  color = Color.RED;
  friction = 0.1f;
  bounce = 0.2f;
  liquid = false;
}

or you need to call a private method in the derived class (remove the abstract init() from Tile):

public TestTile(int x, int y, int z) {
  super(x, y, z);
  init();
}

private void init() {
  color = Color.RED;
  friction = 0.1f;
  bounce = 0.2f;
  liquid = false;
}

Are you sure members are the right implementation here? Maybe abstract methods (getters) may be better here to declare a behavior and implement it in the subclass?

public abstract class Tile {
  public int x;
  public int y;
  public int z;

  public Tile(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  public abstract Color getColor();
  public abstract float getFriction();
  public abstract float getBounce();
  public abstract boolean isLiquid();
}

public class TestTile extends Tile {

  public TestTile(int x, int y, int z) {
    super(x, y, z);
  }

  public Color getColor() {
    return Color.RED;
  }

  public float getFriction() {
    return 0.1f;
  }

  public float getBounce() {
    return 0.2f;
  }

  public boolean isLiquid() {
    return false;
  }

}
夜访吸血鬼 2024-11-11 10:36:37

构造函数不是继承的,因此在创建 TestTile 对象时不会调用 Tile 的三参数构造函数。您需要从 TestTile 构造函数显式调用三参数 Tile 构造函数,就像您所说的那样,但对 super(x,x,x) 的调用必须是 TestTile 构造函数的第一个语句。

正如 Matt Ball 所说,在删除 void 返回类型之前,您的 Tile“构造函数”并不是真正的构造函数。

Constructors are not inherited, so the three-parameter constructor of Tile is not invoked when you create your TestTile object. You need to explicitly call the three-parameter Tile constructor from a TestTile constructor, like you said you did try, but that call to super(x,x,x) must be the first statement of the TestTile constructor.

And like Matt Ball said, your Tile "constructor" isn't really a constructor until you remove the void return type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文