java在第二个类中找不到构造函数

java在第二个类中找不到构造函数,java,class,constructor,compiler-errors,Java,Class,Constructor,Compiler Errors,我有一个简单板类,由陆地扩展,然后由单位扩展。每当我尝试编译单元时,都会出现以下错误: 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

我有一个简单板类,由陆地扩展,然后由单位扩展。每当我尝试编译单元时,都会出现以下错误:

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);

    }
    }

您可以将
单位
土地
等级更改为此等级:

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(intx,inty)
的构造函数,该构造函数内部没有任何代码(只是一个空构造函数),这样它将删除
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);

    }
}
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];
    }
}

Land.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);

    }
}
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.java
更改为
Unit.java

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

思想很简单:

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

它是通过超级(…)语句完成的:

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(…)语句,编译器将假定您正在调用父类的无参数构造函数

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

在单元的构造函数中,没有super(…)语句,这意味着它将尝试调用导致问题的Land的no-arg构造函数(它不存在)


编辑:
虽然有点离题,但在我看来,你对继承权的使用毫无意义。为什么“土地”是一个“简单板”?为什么“单位”是一个“土地”?他们似乎是一种“有-有”关系,而不是“是-有”对我来说。考虑重新设计你的应用程序,否则你将面临更奇怪的问题:< /P>我们需要更多的代码来回答你的问题。请包括你所有三个类的所有构造函数。谢谢。<代码> SimpleBoard <代码>不是<代码>接口<代码>你为什么要在类<代码>土地< /代码>?Crazenezz Sor中实现它?这只是测试代码的一部分,不是问题的一部分。基本上,这是一个自制项目,试图在java中重新创建棋盘游戏。单位需要在土地上占据一个空间(就像土地类中的占领阵列一样),所以为了让单位识别土地,我让单位扩展土地。如果有更好的方法,请让我知道。确实这样做了,将两个构造函数都添加到两个类中,但仍然会出现相同的错误。我不知道关于构造函数的事,tho,谢谢!我在评论中对项目做了解释,如果你想查看的话这是我的答案。根据你的解释更新了我的答案,希望能帮助你解决问题。我将
Units
类更改为
Unit
类。我同意你的编辑;但是我如何使这些类可以彼此对话而不成为彼此的孩子呢?我整个夏天都没有做太多Java,所以我很生疏。你有参考文献吗引用一个对象,然后您可以与该对象“对话”。例如,在板中,有
列表
,然后您可以简单地执行
单位。get(0).doSomething()让你自己习惯于用面向对象的方式思考。显然你完全误解了事物的互动方式。先准备好一点,然后再写一些简单的东西。顺便说一下,这已经偏离主题了。如果它已经回答了你的问题,请考虑接受答案。