简体   繁体   English

对象初始化(Java)

[英]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. 如果我想为player1设置名称,可以通过调用Team()方法来设置。 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? 但是,如果我随后想为player2设置一个名称,它将重置玩家1的名称,因为每次调用该方法时都会创建一个新的player1和player2对象,对吗?

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? 所以我的问题是,如果我只想在第一次运行Team()方法时仅初始化player1和player2对象,我将如何实现? Because if I moved the object initializations out of the Team class, I wouldn't have access to getName and setName. 因为如果将对象初始化移出Team类,则将无法访问getName和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 . 您应该了解TeamPlayer之间的关系。

In your current implementation, a Team have two Player s, I would move the initialization to a constructor: 在您当前的实现中,一个Team有两个Player ,我将初始化移到构造函数中:

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. player1player2的初始化应在某些驱动程序中完成,该类应负责初始化Player并将它们设置为团队。

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. 设置/获取玩家的名字应该在Player类中进行,而不是在Team类中进行,因为Team仅对Team负责,而不对Player的详细信息负责。

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. 我将Team类更改为包含X Player ,而不仅仅是2个,将来扩展该类会更容易。

Important note: Use equals to compare Strings, == compares references and not String's content. 重要说明:使用equals比较字符串, ==比较引用而不是String的内容。

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. 这样,您可以为读入的每个名字添加任意数量的玩家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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