这样,我可以打印该类中每个对象的名称列表,但是我希望能够在锦标赛类的方法中调用变量,然后在GameUI中打印包含该变量的方法名称取决于用户输入。
public class Reserve {
private List<Dragon> dragonReserve;
private List<Wizard> wizardReserve;
private List<Warrior> warriorReserve;
//private List<Champions> champions;
public Reserve() {
wizardReserve = new ArrayList<>();
dragonReserve = new ArrayList<>();
warriorReserve = new ArrayList<>();
//champions = new ArrayList<>();
wizardReserve.add(new Wizard("Ganfrank", 7, true, "transmutation"));
wizardReserve.add(new Wizard("Rudolf", 6, true, "invisibility"));
wizardReserve.add(new Wizard("Neon", 2, false, "translocation"));
warriorReserve.add(new Warrior("Elblond", 1, 150, "sword"));
warriorReserve.add(new Warrior("Flimsi", 2, 200, "bow"));
warriorReserve.add(new Warrior("Argon", 9, 900, "mace"));
dragonReserve.add(new Dragon("Drabina", 7, false));
dragonReserve.add(new Dragon("Golum ", 7, true));
dragonReserve.add(new Dragon("Xenon ", 7, true));
}
public List<Dragon> getDragonReserve() {
return dragonReserve;
}
public List<Wizard> getWizardReserve() {
return wizardReserve;
}
public List<Warrior> getWarriorReserve() {
return warriorReserve;
}
}
public class GameUI {
private static Scanner myIn = new Scanner(System.in);
public static void main(String[] args) {
int choice;
String playerName;
String output = "";
int result = -1;
try {
System.out.println("Enter player's name");
String s = myIn.nextLine();
//myIn.nextLine();
choice = 100;
while (choice != 0) {
choice = getMenuItem();
if (choice == 1) {
tr.getReserve().getDragonReserve().forEach(System.out::println);
tr.getReserve().getWizardReserve().forEach(System.out::println);
tr.getReserve().getWarriorReserve().forEach(System.out::println);
}
public class Tournament implements TOC
{
public String getReserve() {
Reserve reserve = new Reserve();
return reserve;
}
}
答案 0 :(得分:0)
我不完全了解您想要什么,但是我认为这可以解决您的问题。
预备班。
public class Reserve {
private List<Dragon> dragonReserve;
...
public Reserve() {
dragonReserve = new ArrayList<>();
dragonReserve.add(new Dragon("Drabina", 7, false));
...
}
public List<Dragon> getDragonReserve() {
return dragonReserve;
}
...
}
GameUI类。
public static void main(String[] args) {
Tournament tournament = new Tournament();
...
if (choice == 1) {
tournament.getReserve().getDragonReserve().forEach(System.out::println);
}
...
}
锦标赛类。
public class Tournament implements TOC {
public Reserve getReserve() {
Reserve reserve = new Reserve();
return reserve;
}
}
还可以在对象中覆盖toString()函数。