0

我有一个由 Land 扩展的 SimpleBoard 类,然后由 Units 扩展。每当我尝试编译 Units 时,都会收到此错误:

cannot find symbol
symbol  : constructor Land()
location: class Land
    public Units(int x, int y){

想出了什么问题?

编辑:对不起,我很着急!这是完整的代码:

public class Units extends Land{

    private int attack;

    public Units(int x, int y) {

        if(x==1){
            attack = 1;
        }
        else if(x==2)
            attack = 2;
        else if(x==3)
            attack = 4;
        ar[y].AddUnit(this);
    }

    public int getAttack(){
        return attack;
    }
    }

和土地

public class Land extends SimpleBoard {
    private boolean barrel = false;
    private boolean crown = false;
    private boolean castle = false;
    private boolean stronghold = false;
    private int num;
    private int array = 0;
    public int[] adjacent;
    private Units [] Occupy = new Units [4];

    public Land(int z, int x, int c, int v, int b, int[] array){
        if(z == 1)
            barrel = true;
        if(x == 1)
            crown = true;
        if(c == 1)
            castle = true;
        if (v==1)
            stronghold = true;
        num = b;
        adjacent = array;
    }

    public void addUnit(Units x){
        Occupy[array] = x;
        array++;
    }

    public boolean checkB(){
        return barrel;
    }


    public int getAdj(int i){
        return adjacent[i];
    }
    }

和板

public class SimpleBoard {
    public static Land[] ar = new Land[3];
    public static void main(String[] args){
    Land One = new Land(1,0,0,0,1, new int[]{2, 3});
    Land Two = new Land(0,1,0,0,2, new int[]{1, 3});
    Land Three = new Land(0,0,1,0,3, new int[]{1, 2});
    ar[0] = One;
    ar[1] = Two;
    ar[2] = Three;

    Units Footman = new Units(1, 1);
    Units Knight = new Units(2, 3);
    Units Siege = new Units(3, 2);

    }
    }
4

2 回答 2

1

您可以将您的UnitsLand班级更改为此:

public class Units extends Land {

    private int attack;

    public Units(int z, int x, int c, int v, int b, int[] array) {
        super(z, x, c, v, b, array);
    }

    public Units(int x, int y) {

        if (x == 1) {
            attack = 1;
        } else if (x == 2) {
            attack = 2;
        } else if (x == 3) {
            attack = 4;
        }
        ar[y].addUnit(this);
    }

    public int getAttack() {
        return attack;
    }
}

public class Land extends SimpleBoard {

    // Declared variable

    public Land(int x, int y) {

    }

    // Rest of the code...
}

由于继承,您将在Units类中声明的构造函数也必须存在于Land类中。

在您的类中添加一个构造函数,Land调用其中Land(int x, int y)没有任何代码(只是一个空白构造函数),以便它将删除您的Units类中的错误。这不是最佳做法,因为我不知道您要做什么。如果你赶时间,你可以试试这个,但如果你有时间,请简要说明你的申请目的。

更新:

SimpleBoardGame.java

public class SimpleBoardGame {

    private static Land[] ar = new Land[3];

    public static Land[] getAr() {
        return ar;
    }

    public static void main(String[] args) {
        Land One = new Land(1, 0, 0, 0, 1, new int[]{2, 3});
        Land Two = new Land(0, 1, 0, 0, 2, new int[]{1, 3});
        Land Three = new Land(0, 0, 1, 0, 3, new int[]{1, 2});
        ar[0] = One;
        ar[1] = Two;
        ar[2] = Three;

        // When you pass the parameter for 'y' please make sure it already minus by one.
        // If not, will occur the 'java.lang.ArrayIndexOutOfBoundsException'
        Unit Footman = new Unit(1, 0);
        Unit Knight = new Unit(2, 2);
        Unit Siege = new Unit(3, 1);

    }
}

土地.java

public class Land {

    // For boolean variable, no need to set the value as "false" since it default is "false".
    private boolean barrel;
    private boolean crown;
    private boolean castle;
    private boolean stronghold;
    private int num;
    private int array = 0;
    public int[] adjacent;
    private Unit[] Occupy = new Unit[4];

    public Land(int x, int y) {
        // Empty constructor...
    }

    public Land(int z, int x, int c, int v, int b, int[] array) {
        if (z == 1) {
            barrel = true;
        }
        if (x == 1) {
            crown = true;
        }
        if (c == 1) {
            castle = true;
        }
        if (v == 1) {
            stronghold = true;
        }
        num = b;
        adjacent = array;
    }

    public void addUnit(Unit x) {
        Occupy[array] = x;
        array++;
    }

    public boolean checkB() {
        return barrel;
    }

    public int getAdj(int i) {
        return adjacent[i];
    }
}

Unit.java (从 更改Units.javaUnit.java)

public class Unit extends Land {

    private int attack;

    public Unit(int z, int x, int c, int v, int b, int[] array) {
        super(z, x, c, v, b, array);
    }


    public Unit(int x, int y) {
        super(x, y);

        if (x == 1) {
            attack = 1;
        } else if (x == 2) {
            attack = 2;
        } else if (x == 3) {
            attack = 4;
        }

        addUnit(y);
    }

    /**
     * Overload method of the addUnit() of Land class.
     * Better not use "this" inside the constructor.
     * 
     * @param y
     */
    public final void addUnit(int y) {
        SimpleBoardGame.getAr()[y].addUnit(this);
    }

    public int getAttack() {
        return attack;
    }
}

注意:请确保你没有继承SimpleBoardGame作为类中的主类Land,如果你想访问主类中的变量,只需为此创建 setter 和 getter。您可以像这样访问(请参阅类中的SimpleBoardGame.getAr()[y].addUnit(this);内部方法。addUnit(int y)Unit

于 2012-07-31T02:27:22.917 回答
0

这个想法很简单:

子继承自父。在子构造函数中,您需要首先通过调用父构造函数来构造对象的“父”部分,然后再构造您的子部分。

它由 super(...) 语句完成:

class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }
}

class Human extends Animal {
    String familyName;

    public Human(String name, String familyName) {
        super(name);    // HERE!!!! means calling Animal's Animal(String) ctor
        this.familyName = familyName;
    }
}

如果您没有显式添加 super(...) 语句,编译器将假定您正在调用 Parent 类的无参数构造函数。

对于没有定义构造函数的类,编译器会为其添加一个默认的无参数构造函数。但是,由于您的 Land 定义了 public Land(int z, int x, int c, int v, int b, int[] array) 构造函数,这意味着您在 Land 中将没有无参数构造函数。

Unit 的构造函数中没有 super(...) 语句,这意味着它会尝试调用 Land 的无参数构造函数(不存在),从而导致问题


编辑:虽然有点离题,但在我看来,您对继承的使用没有任何意义。为什么“Land”是“SimpleBoard”?为什么“单位”是“土地”?对我来说,它们似乎是一种“有”的关系,而不是“是”的。考虑重新设计您的应用程序,否则您将面临更奇怪的问题

于 2012-07-31T03:19:47.943 回答