Java 对象的变量由另一个类中的方法更改

Java 对象的变量由另一个类中的方法更改,java,object,instance-variables,Java,Object,Instance Variables,我在我的CardGames类中创建了一个简单的方法,它复制一个纸牌游戏来使用条件语句。我从一个单独的玩家类调用该方法,因为该玩家基于该卡获得/失去积分。我希望该方法能够更改player objects points变量 我希望发生的是,当playSimpleCardGame被Player对象调用时,该方法会更改Player对象的点 但当我运行它时,点不会改变。我尝试了扩展/实现这两个类,即在黑暗中拍摄。我还在CardGames类中创建了一个实例变量points,但是Player对象没有点作为变量

我在我的CardGames类中创建了一个简单的方法,它复制一个纸牌游戏来使用条件语句。我从一个单独的玩家类调用该方法,因为该玩家基于该卡获得/失去积分。我希望该方法能够更改player objects points变量

我希望发生的是,当playSimpleCardGame被Player对象调用时,该方法会更改Player对象的点

但当我运行它时,点不会改变。我尝试了扩展/实现这两个类,即在黑暗中拍摄。我还在CardGames类中创建了一个实例变量points,但是Player对象没有点作为变量。我错过了什么

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;
    }
}

首先,没有必要通过Player类来扩展CardGames类。第二,即使你想这样做,这将是一个糟糕的设计。我不谈设计部分。以下代码应能回答您的问题:

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;
    }
}

您可能已经命名了两个变量点,即使是在同一个对象上,但它们不是同一个变量。您的代码无法编译,因为CardGames中没有点。。。尝试将点从玩家移动到纸牌游戏…这会使方法运行,但不会给玩家对象点,这些点仍然在纸牌游戏中。我想能够调用一些行steve.points.Thank的代码。当我运行它时,我得到消息,无法对非静态字段点进行静态引用。没有任何东西被声明为静态的。playSimpleCardGame是否在其参数中进行静态引用?您在哪一行得到这个错误@gunsnfloyd?我在Player的System.out行得到它。我现在得到以下消息:线程主java.lang.NullPointerException中的异常。有没有可能使用纸牌游戏作为玩家的界面?这个练习的目的是向我的学生展示对象如何使用其他类的方法,并让这些方法影响对象。@gunsnfloyd它现在肯定可以工作了。对不起,我犯了很多错误。
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;
    }
}