package MainFunction;
public class Weapon {
Weapon(String name, int damage, String[] effects){
this.name = name;
this.damage = damage;
this.effects = effects;
}
private int damage;
public String name;
private String[] effects;
public void addEffect(String effect){
effects[effects.length] = effect;
}
}
class Gun extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private String ammoType;
private String bulletMaterial;
private int barrelLength;
private String fireType;
Gun( String name, int damage, String[] effects, String fireType, String ammoType, String bulletMaterial, int barrelLength) {
super(name, damage, effects);
// TODO Auto-generated constructor stub
this.name = name;
this.damage = damage;
this.effects = effects;
this.fireType = fireType;
this.ammoType = ammoType;
this.type = "ranged";
this.bulletMaterial = bulletMaterial;
this.barrelLength = barrelLength;
}
}
class Sword extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private int bladeLength;
private int hiltLength;
private String bladeMaterial;
Sword(String name, int damage, String[] effects, int barrelLength, int hiltLength, String bladeMaterial){
super(name, damage, effects);
this.name = name;
this.damage = damage;
this.effects = effects;
this.type = "melee";
this.bladeLength = bladeLength;
this.hiltLength = hiltLength;
this.bladeMaterial = bladeMaterial;
}
}
m_one中存在语法错误。它说: 令牌上的语法错误",",此令牌后预期的表达式
我不明白这是从哪里来的。这是我的构造函数:
{{1}}
构造函数不会返回任何错误,但是当我使用它时它会发生错误。有什么问题?
答案 0 :(得分:2)
您正尝试使用此表达式创建并传递字符串数组:
["Bleeding"]
但是,这不是用于创建数组的Java语法。您必须显式创建数组,指定类型,以及用于包含内容的大括号{}
。
new String[] {"Bleeding"}
如果您愿意,可以有多个元素:
new String[] {"Bleeding", "Damaging"};
此外,稍后在Weapon
的{{1}}方法中,您尝试通过指定超出数组长度的元素来对数组进行长度调整。
addEffect
在Java中,数组一旦创建就具有固定的大小,因此这将抛出effects[effects.length] = effect;
。如果您需要可变长度数组,请将ArrayIndexOutOfBoundsException
替换为String[]
。首先,在构造函数中:
List<String>
然后声明Weapon(String name, int damage, List<String> effects){
:
effects
然后定义List<String> effects;
:
addEffect
您无需在effects.add(effect);
中创建数组,而是需要创建main
的实现并将其传入。
List
答案 1 :(得分:0)
您需要在m_one
声明中使用大括号而不是方括号,周围&#34;出血&#34;。