这是我的战士班。我想在这堂课上有一系列的战士。 我们的想法是能够调用这样的方法 - > warrior.select(1)它将获得在索引1处创建的战士。希望这是有道理的。 请向我解释为什么会发生这种错误。
Exception in thread "main" java.lang.StackOverflowError
at pking.Warrior.<init>(Warrior.java:42)
代码
package pking;
public class Warrior {
private String name;
private int age;
private String call;
private int attackPower;
private String weapon;
public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
name = myName;
age = myAge;
call = myCall;
attackPower = myAttackPower;
weapon = "";
Warrior[] warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
}
//Prints warriors name
public void name() {
System.out.println(name);
}
//Prints warriors age
public void age() {
System.out.println(age);
}
//Prints warriors call
public void warriorsCall() {
System.out.println(call);
}
//Prints warriors attack power
public void attackPower() {
System.out.println(attackPower);
}
//Equips warriors weapon and prints message
public void equip(String myWeapon) {
weapon = myWeapon;
System.out.println("Equiped the: " + weapon);
}
//Prints warriors weapon
public void weapon() {
System.out.println(weapon);
}
}
答案 0 :(得分:6)
您正在以递归方式调用它时,构造函数正在运行无限循环。在构造函数中检查以下行:
Warrior[] warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
对于每个Warrior数组项实例化,你调用相同的构造函数,它再次尝试创建数组并初始化项,这一直持续到堆栈溢出。
更好的设计策略是创建一个新类,比如Legion
,它将包含Warrior
s的集合:
public class Legion {
Warrior[] warriors;
public Legion() {
warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
}
// getters and setters
}
答案 1 :(得分:0)
我不打算重复StackOverflow
异常的原因,因为已经回答了(构造函数中的递归问题)。
但是,我认为您可能想要的是创建那些 一次 的4个特殊战士而不是Warrior
的每个实例。也许你想以某种方式将它们作为常量。
所以也许是这样的:
public class Warrior {
private static final List<Warrior> MEGA_WARRIORS =
Collections.unmodifiableList(Arrays.asList(
new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000),
new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000),
new Warrior("Gannicus", 30, "SLAYER OF *censored*", 8000),
new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0)
));
public static List<Warrior> getMegaWarriors() {
return MEGA_WARRIORS;
}
private String name;
private int age;
private String call;
private int attackPower;
private String weapon;
public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
name = myName;
age = myAge;
call = myCall;
attackPower = myAttackPower;
weapon = "";
}
//Prints warriors name
public void name() {
System.out.println(name);
}
//Prints warriors age
public void age() {
System.out.println(age);
}
//Prints warriors call
public void warriorsCall() {
System.out.println(call);
}
//Prints warriors attack power
public void attackPower() {
System.out.println(attackPower);
}
//Equips warriors weapon and prints message
public void equip(String myWeapon) {
weapon = myWeapon;
System.out.println("Equiped the: " + weapon);
}
//Prints warriors weapon
public void weapon() {
System.out.println(weapon);
}
}