【问题标题】:java cannot find constructor within second classjava在第二类中找不到构造函数
【发布时间】:2023-03-22 16:11:01
【问题描述】:

我有一个由 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);

    }
    }

【问题讨论】:

  • 我们需要更多代码来回答您的问题。请包括所有三个类的所有构造函数。谢谢。
  • SimpleBoard 不是interface 为什么要在Land 类中实现它?
  • @Crazenezz 抱歉,这只是测试代码的剩余部分,不是问题的一部分
  • 基本上这是一个自制项目,旨在尝试在 java 中重新创建棋盘游戏。 Units 需要在 Land 中占据一个空间(就像 Land 类中的 Occupy 数组中所做的那样),因此为了让 Units 识别 Land,我让 Units 扩展 Land。如果有更好的方法,请告诉我。

标签: java class constructor compiler-errors


【解决方案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);

    }
}

Land.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.java更改为Unit.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);(请参阅Unit 类中的内部方法addUnit(int y)

【讨论】:

  • 确实做到了这一点,将两个构造函数添加到两个类中,但仍然吐出相同的错误。不知道关于构造函数的东西,谢谢!
  • 我在 cmets 中留下了对该项目的解释,如果您想查看的话。
  • 根据您的解释更新了我的答案,希望对您的问题有所帮助。我将Units 类更改为Unit 类。
【解决方案2】:

这个想法很简单:

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

这是通过一个 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 定义了一个公共 Land(int z, int x, int c, int v, int b, int[] array) 构造函数,这意味着您在 Land 中将没有无参数构造函数。

Unit的构造函数中没有super(...)语句,表示会尝试调用Land的无参数构造函数(不存在),导致问题


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

【讨论】:

  • 我同意你的编辑;但是我怎样才能让这些班级可以互相交谈而不是彼此的孩子呢?我整个夏天都没有做太多 Java,所以我很生疏。
  • 您拥有对一个对象的引用,然后您可以与该对象“对话”。例如,在 Board 中,有List<Unit>,那么你可以简单地做units.get(0).doSomething()。让自己习惯于以面向对象的方式思考。显然你完全误解了事物相互作用的方式。多做点准备,先写一些简单的东西,为你的基础打下基础。
  • 顺便说一句,这已经离题了。如果它已经回答了您的问题,请考虑接受它。\
猜你喜欢
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2021-06-14
  • 2010-12-12
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多