创建同一类的多个对象的更简单方法

时间:2016-02-04 17:10:56

标签: java class methods

我有一个播放器类,包含所有的setter&干将。

我必须从键盘上读取5个玩家,我很好,但在创建时:

Player player1 = new Player();

然后询问所有输入。

有什么方法可以增加1来制作Player2,Player3等。

我尝试过++方法。我不知道我究竟要求的是什么。提前抱歉让您感到困惑。

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    Player player1 = new Player();
    player1.getSurname();
    player1.getFirstName();
    player1.getAge();
    player1.getHeight();
    player1.getRank();

    Player player2 = new Player();
    player2.getSurname();
    player2.getFirstName();
    player2.getAge();
    player2.getHeight();
    player2.getRank();

    Player player3 = new Player();
    player3.getSurname();
    player3.getFirstName();
    player3.getAge();
    player3.getHeight();
    player3.getRank();

4 个答案:

答案 0 :(得分:5)

自我记录代码更可取。 get方法应该只是return一个值,而不是执行操作,例如用户输入。

Scanner in = new Scanner(System.in);

int NUM_PLAYERS = 5;

ArrayList<Player> players = new ArrayList<Player>();

for (int i = 0; i < NUM_PLAYERS; i++) {
    Player p = new Player();

    System.out.print("Surname: ");
    String surname = in.nextLine();

    System.out.print("Firstname: ");
    String fname = in.nextLine();

    System.out.print("Age: ");
    int age = in.nextInt();

    System.out.print("Height: ");
    int height = in.nextInt();

    System.out.print("Rank: ");
    int rank = in.nextInt();

    p.setSurname(surname);
    p.setFirstName(fname);
    p.setAge(age);
    p.setHeight(height);
    p.setRank(rank);

    System.out.println("Current Player \n" + p);
    players.add(p);
    in.nextLine(); // clear the buffer
}

System.out.println("All players");
System.out.println(players);

使用完整模型对象和set方法实际分配数据

public class Player {

    private String surname;
    private int height;
    private int rank;
    private int age;
    private String firstName;

    public String getSurname() {

        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Player{" +
                "surname='" + surname + '\'' +
                ", height=" + height +
                ", rank=" + rank +
                ", age=" + age +
                ", firstName='" + firstName + '\'' +
                '}';
    }
}

答案 1 :(得分:1)

你可以这样做。

List<Player> players = new ArrayList<Player>();

然后你可以循环并添加玩家。 Checkout ArrayList documentation

答案 2 :(得分:1)

您可以制作Player个对象

的数组
Player[] players = new Player[5]; //creates an array that can hold 5 Player objects
for (int i = 0; i < 5; i++) {
  players[i] = new Player();
  player.getSurname();
  //place rest of code here
}

答案 3 :(得分:1)

Scanner in = new Scanner(System.in);
List<Player> players = new ArrayList<Player>();
for(...){//decide how many to add
   Player player = new Player();
   player.getSurname();
   player.getFirstName();
   player.getAge();
   player.getHeight();
   player.getRank();
   players.add(player);
}
...//use the Player objects in players