3 回答

TA贡献1825条经验 获得超6个赞
static从type和damage中删除Weapon。static意味着全局类的一个值,而不是每个类实例一个值(这是你想要的)。this.type = type;另外,this.damage = damage;在构造函数中。
public class Weapon {
private String type;
private int damage;
public Weapon(String type, int damage) {
this.type = type;
this.damage = damage;
}
public String getType() {
return type;
}
}
此外,您目前没有使用您的任何值List(您保留了您创建的引用,并通过这些引用进行调用)。并且更喜欢编程而List不是ArrayList类型接口(你可以使用菱形运算符<>)。喜欢,
List<Weapon> weapons = new ArrayList<>();
weapons.add(new Weapon("Desert Eagle", 5));
weapons.add(new Weapon("AK-47", 3));
for (Weapon w : weapons) {
System.out.println(w.getType());
}
产出
Desert Eagle
AK-47
没有找到匹配的内容?试试慕课网站内搜索吧
添加回答
举报