我有一个由Land扩展的SimpleBoard类,然后由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);
}
}
答案 0 :(得分:1)
您可以将Units
和Land
课程更改为此课程:
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
的{{1}}类中添加一个构造函数,其中没有任何代码(只是一个空白构造函数),这样它就会删除Land(int x, int y)
类中的错误。这不是最好的做法,因为我不知道你想做什么。如果你赶时间,你可以尝试这个,但如果你有时间,请简要说明你的申请目的。
<强> SimpleBoardGame.java 强>
Units
<强> 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);
}
}
Unit.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];
}
}
更改为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
类中的主类,如果要访问主类中的变量,只需创建setter和getter为了那个原因。您可以像Land
这样访问SimpleBoardGame.getAr()[y].addUnit(this);
(请参阅addUnit(int y)
类中的方法Unit
。
答案 1 :(得分:0)
这个想法很简单:
Child继承自Parent。在Child构造函数中,您需要首先通过调用Parent的构造函数来构造对象的“Parent”部分,然后构造子部件。
由超级(...)语句完成:
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”?为什么“单位”是“土地”?他们似乎是一个“有一个”的关系而不是“是一个”给我。考虑重新设计您的应用程序,否则您将面临更多奇怪的问题