对象的变量在另一个 class 中被方法更改

Objects' variables changed by method in another class

我在我的 CardGames class 中创建了一个简单的方法,它复制纸牌游戏来玩转条件语句。我从一个单独的玩家 class 调用该方法,因为玩家 earns/loses 根据卡片得分。我希望该方法能够更改播放器对象点变量。

我想要发生的是,当 playSimpleCardGame 被 Player 对象调用时,该方法会更改 Player 对象的点数。

但是当我 运行 它的点数没有改变。我试过 extending/implementing 两种 class 方法(即在黑暗中拍摄)。我还在 CardGames class 中创建了一个实例变量 points 但是 Player 对象没有 points 作为变量。我错过了什么?

public class Player 
{
    private int points;

    public static void main(String[] args)
    {
        CardGames steve = new CardGames();
        System.out.println(steve.playSimpleCardGame("red"));
        System.out.println(steve.playSimpleCardGame("red"));
        System.out.println(steve.playSimpleCardGame("black"));
        System.out.println(steve.playSimpleCardGame("black"));
        System.out.println(steve.points);
    }

}

public class CardGames
{

    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color)
    {
        if (color.equalsIgnoreCase("red"))
            return points = points + 1;
        else 
            return points = points - 2;
    }
}

public class Player
{
    private int points;

    public Player(){
        points=0;
    }

    public static void main(String[] args)
    {
        CardGames game = new CardGames();
        Player steve = new Player();
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(steve.points);
    }

    public int getPoints() {
        return points;
    }

    public void addPoints(int p) {
        this.points = points + p;
    }

}

public class CardGames
{
    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color, Player player)
    {
        if (color.equalsIgnoreCase("red"))
        {
            player.addPoints(1);
            return player.getPoints();
        }
        else
        {
            player.addPoints(-2);
            return player.getPoints();
        }
    }
}

首先,Player class 不需要扩展 CardGames class。其次,即使你想做,这也是一个糟糕的设计。我不会进入设计部分。以下代码应该可以回答您的问题:

public class Player
{
    public Integer points;

    public Player(){
        points=0;
    }

    public static void main(String[] args)
    {
        CardGames game = new CardGames();
        Player steve = new Player();
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(steve.points);
    }
}

public class CardGames
{
    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color, Player player)
    {
        if (color.equalsIgnoreCase("red"))
            return player.points = player.points + 1;
        else 
            return player.points = player.points - 2;
    }
}