题目:
创建一个Hero类代表一个英雄,其中包含名字和战斗力两个成员变量。使用一个集合存储五位英雄,然后遍历集合展示所有英雄信息。
代码
Hero类
public class Hero {
private String name;//姓名
private int attack;//攻击力
public Hero() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public Hero(String name, int attack) {
super();
this.name = name;
this.attack = attack;
}
}
HeroTest类
import java.util.ArrayList;
public class HeroTest {
public static void main(String[] args) {
// 创建一个集合,代表一个队伍,有五个英雄
ArrayList<Hero> team = new ArrayList<>();
//创建五个英雄对象
Hero one = new Hero("刘禅",123);
Hero two = new Hero("后羿",1