【问题标题】:How do I create new objects in a switch如何在交换机中创建新对象
【发布时间】:2021-06-17 07:55:38
【问题描述】:

我是 Java 新手,我正在尝试创建一个程序,您可以在其中注册会员加入俱乐部,但我在尝试添加/创建人员时遇到了困难。

我似乎无法弄清楚如何在 if 语句或 switch 中继续创建类的新对象。 (比如让我们说通过切换并添加 10 个新成员)。

public static void main(String[] args) throws NoSuchMethodException {
        System.out.println("Opties: \n" +
                           "1) Add a member to the club.");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        scanner.nextLine();

    do{
        switch(choice){
        case 1:
            System.out.println("New member name: ");
            String newMemberName = scanner.nextLine();

            System.out.println("New member age: ");
            int newMemberAge = scanner.nextInt();
            scanner.nextLine();

            System.out.println("""
                    New member category:\s
                    A) Player
                    B) Coach""");
            String categoryChoice = scanner.nextLine();

            if (categoryChoice.equalsIgnoreCase("a")){


            } else if(categoryChoice.equalsIgnoreCase("b")){


            } else break;

        case 2:
            ///temp
        }
      } while(choice != 3);

这是类+子类

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

class Player extends Person {

    public Player(String name, int age) {
            super(name, age);
        }
    }

class Trainer extends Person {

     public Trainer(String name, int age) {
            super(name, age);
        }
}

【问题讨论】:

  • 如果您打算创建多个对象实例,请在 while 循环之外创建一个数据结构(例如 ArrayList),并且每次创建新成员时都将新成员附加到该列表中。

标签: java class object oop polymorphism


【解决方案1】:

您不应该在 if 语句中创建变量,您应该使用默认值 null 预先创建变量,然后使用适当的对象在每个 if 语句中更改变量的值:

public static void main(String[] args) throws NoSuchMethodException {
    System.out.println("Opties: \n" +
                           "1) Add a member to the club.");
    Scanner scanner = new Scanner(System.in);
    int choice = scanner.nextInt();
    scanner.nextLine();

    Person person = null; //needs to be the super class

    do{
        switch(choice){
        case 1:
            System.out.println("New member name: ");
            String newMemberName = scanner.nextLine();

            System.out.println("New member age: ");
            int newMemberAge = scanner.nextInt();
            scanner.nextLine();

            System.out.println("""
                    New member category:\s
                    A) Player
                    B) Coach""");
            String categoryChoice = scanner.nextLine();

            if (categoryChoice.equalsIgnoreCase("a")){
                person = new Player(newMemberName, newMemberAge);
            } else if(categoryChoice.equalsIgnoreCase("b")){
                person = new Trainer(newMemberName, newMemberAge);
            } else break;

        case 2:
            ///temp
        }
    } while(choice != 3);
}

由于您是在循环中创建对象,因此创建 ArrayList<Person> 而不是单个变量并在每次迭代期间向其中添加一个新对象会更有意义:

    ArrayList<Person> people = new ArrayList<Person>(); //needs to be the super class - an empty list

   do { ...
       if (categoryChoice.equalsIgnoreCase("a")){
                people.add(new Player(newMemberName, newMemberAge));
       } else if(categoryChoice.equalsIgnoreCase("b")){
                people.add(new Trainer(newMemberName, newMemberAge));
       } else break;
   } while(choice != 3);

【讨论】:

  • 最好有一个带有参数categoryChoice, newMemberName, newMemberAge 的辅助方法,它只包含那个 if 序列,并立即创建并返回这些人。 1)因为您通常需要在不止一个地方使用它,以及 2)因为行为/状态在立即返回时更加明显,尤其是在最后退出 if 链时。
猜你喜欢
  • 2023-01-01
  • 2018-07-21
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2011-03-30
  • 2014-09-04
  • 2022-11-05
  • 2020-11-18
相关资源
最近更新 更多