Setter 方法在彼此的类中设置字段

发布于 2025-01-16 22:06:50 字数 518 浏览 1 评论 0原文

我有两个类,玩家类和团队类。一个球员有一个 team 字段,一个球队有一个playerList 字段,包含该球队的球员。当玩家加入队伍时,玩家的队伍字段应设置为该队伍,并且队伍的playerList应添加加入的玩家对象。这是我到目前为止所拥有的,但这不是非常递归吗?我该如何解决这个问题?下面提供了示例代码。


public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player){
      playerList.add(player);
      player.joinTeam(this);
   }

}

public class Player {
   
   private Team team;
   
   public void joinTeam(Team team) {
      this.team = team;
      team.addPlayer(this)
   }

}


I have two classes, Player and Team. A player has a team field, and a team has a playerList field, containing the players on that team. When a player joins a team, the player's team field should be set to that team, and the team's playerList should add the player object joining. This is what I have so far, but is this not very recursive? How can I solve this problem? Example code is provided below.


public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player){
      playerList.add(player);
      player.joinTeam(this);
   }

}

public class Player {
   
   private Team team;
   
   public void joinTeam(Team team) {
      this.team = team;
      team.addPlayer(this)
   }

}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2025-01-23 22:06:50

原则上,对于特定类型的操作,您应该只拥有一个函数。如果您想将玩家添加到团队中,类似的东西

public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player) {
      playerList.add(player);
      player.setTeam(this);
   }
}

public class Player {
   
   private Team team;
   
   public void setTeam(Team team) {
      this.team = team;
   }
}

会更适合您的任务。您只需调用一个方法 (addPlayer),您希望发生的两项操作就会发生。

In principle, you should only really have one function for a specific type of action. If you want to add a Player to a Team, something like

public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player) {
      playerList.add(player);
      player.setTeam(this);
   }
}

public class Player {
   
   private Team team;
   
   public void setTeam(Team team) {
      this.team = team;
   }
}

would be better suited for your task. You only call one method (addPlayer) and both actions you want to have happen, will happen.

意犹 2025-01-23 22:06:50

那么,为了使您的代码正常工作,您应该从方法 joinTeam() 中删除 team.addPlayer(this) 。但现在您只是为属性设置一个值,因此它应该是 setTeam()

public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player){
      playerList.add(player);
      player.joinTeam(this);
   }

}

public class Player {
   
   private Team team;
   
   public void setTeam(Team team) {
      this.team = team;
   }

}

Well, to make your code work, you should just remove team.addPlayer(this) from the method joinTeam(). But now you are just setting a value to your attribute, so it should be a setTeam().

public class Team {

   private ArrayList<Player> playerList;
   
   public void addPlayer(Player player){
      playerList.add(player);
      player.joinTeam(this);
   }

}

public class Player {
   
   private Team team;
   
   public void setTeam(Team team) {
      this.team = team;
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文