简体   繁体   中英

Object initialization (Java)

Say you have a class called team:

public class Team {

    public void Team() {
        Player player1 = new Player();
        Player player2 = new Player();

        if (player1.getName == "") {
            player1.setName = console.next();
        }

        else if (player2.getName == "") {
            player2.setName = console.next();
        }

    }
}

And another class called player:

public class Player {
    private String name;

    public void setName(String inputName) {
        name = inputName;
    }

    public String getName() {
    return name;
    }
}

If I wanted to set a name for player1, I could do so by calling the Team() method. But if I then want to set a name for player2, it's going to reset the name of player 1 as it is making a fresh player1 and player2 object each time the method is called, right?

So my question is, if I want to only initialize the player1 and player2 objects the first time I run the Team() method, how would I achieve that? Because if I moved the object initializations out of the Team class, I wouldn't have access to getName and setName.

I'm quite new to this so any help such as an alternate way of achieving this (without the use of arrays) would be greatly appreciated! Thanks.

You should understand the relation between a Team and a Player .

In your current implementation, a Team have two Player s, I would move the initialization to a constructor:

public class Team {

    private Player player1;
    private Player player2;

    public Team(Player player1, Player player2) {
        this.player1 = player1; 
        this.player1 = player2;   
    }
}

The initialization of player1 and player2 should be done in some driver , a class that should be responsible of initializing Player s and setting them to a team.

Setting/getting the player's name should be done in the Player class, not in the Team class, as Team is only responsible for the team, not for the Player s details.

I would change Team class to contain X Player s and not only 2, it'll be easier to expand this class in the future.

Important note: Use equals to compare Strings, == compares references and not String's content.

public class Team {

Player player1, player2;

public void Team() {
    player1 = new Player();
    player2 = new Player();
}

public void Team(Player p1, Player p2) {
    player1 = p1;
    player2 = p2;
}

public void setPlayer1(String name) {    
    if (!name.isEmpty()) {
        player1.setName = name;
    }
}

public void setPlayer2(String name) {    
    if (!name.isEmpty()) {
        player2.setName = name;
    }
}

public String getPlayer1Name() {
    get player1.getName();
}

public String getPlayer2Name() {
    get player2.getName();
}

then do something like

Team team = new Team();
team.setPlayer1("john");
team.setPlayer2("jane");

or

Team team = new Team("john", "jane");

that's how you would properly write the code,

If you assume a Team could have any number of Players and you build up a team by adding players, you can write it this way.

public class Team {
    private final List<Player> players = new ArrayList<>();
    public void add(Player player) { players.add(player); }
}

public class Player {
    final String name;
    public Player(String name) { this.name = name; }
    public String getName() { return name; }
}

// in your main or another method.
Team team = new Team();
while(/* more players */) {
    team.add(new Player(console.next()));
}

This way you can add any number of players for each name you read in.

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