gpt4 book ai didi

java - 需要 Player 类中的玩家对象

转载 作者:行者123 更新时间:2023-12-02 04:34:55 25 4
gpt4 key购买 nike

在 initPlayerOnJoin 中,我需要访问玩家的余额。这很可能是一个简单的修复,但我仍在学习 OOP 的概念。

 public class Player {

private String name;
private Stock stock;
private Integer balance;


public Player(String name) {

this.setName(name);

initPlayerOnJoin(this);
}

private static void initPlayerOnJoin(Player player) {
player.addBalance(500);
}
public void addBalance(Integer amount) {
balance += amount;
}
}

最佳答案

 public class Player {

private String name;
private Stock stock;
private Integer balance;


public Player(String name) {

this.setName(name);

initPlayerOnJoin(this);
}

//To get the balance
public Integer getBalance() { return balance; }

private static void initPlayerOnJoin(Player player) {
player.addBalance(500);
}
public void addBalance(Integer amount) {
balance += amount;
}
}

关于java - 需要 Player 类中的玩家对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30958039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com