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

Aar*_*bei 0 java constructor class subclass abstract

我没有很多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();
Run Code Online (Sandbox Code Playgroud)

而这个子类:

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

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

Tile tile = new TestTile(0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

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

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

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

如果它与任何东西有关,我使用的是32位Ubuntu Linux 11.04.

谢谢.

sla*_*dau 6

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

public Tile(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
        init();
    }
Run Code Online (Sandbox Code Playgroud)