简体   繁体   中英

An easier way to create multiple objects of the same class

I have a Player class with all the setters & getters.

I have to read in 5 Players from the keyboard which I'm fine with but when creating the :

Player player1 = new Player();

Then asking for all the inputs.

Is there any way I can just increment the 1 to make a Player2, Player3, etc.

I have tried the ++ method. I don't properly know what exactly it what I'm asking for. Sorry in advance for the confusion.

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();

Self documenting code is preferable. get methods should simply return a value, not perform an action, such as user input.

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);

Use a full model object and set methods to actually assign the data

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 + '\'' +
                '}';
    }
}

You could do this.

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

Then you could loop in and add players. Checkout ArrayList documentation

You can make an array of Player objects

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
}
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM