【发布时间】: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