其他类中的java调用方法不';无法识别已更改的变量

其他类中的java调用方法不';无法识别已更改的变量,java,class,methods,Java,Class,Methods,我正在开发一个简单的基于文本的RPG游戏。目前,我正在开发一个系统,将玩家的进度保存到一个文本文件中,可以重新打开并从中加载。这就是我遇到问题的地方。当调用返回类中单个变量值的getter时,getter返回我设置的默认值0,并且不知道我在整个游戏中更改了它的值 字符类 public class Character { private int _strength = 0; //initialize the strength variable public Character()

我正在开发一个简单的基于文本的RPG游戏。目前,我正在开发一个系统,将玩家的进度保存到一个文本文件中,可以重新打开并从中加载。这就是我遇到问题的地方。当调用返回类中单个变量值的getter时,getter返回我设置的默认值0,并且不知道我在整个游戏中更改了它的值

字符类

public class Character
{
    private int _strength = 0; //initialize the strength variable

    public Character() //constructor
    {  }

    public void setStrength(int strength)  //sets the value of strength
    { _strength = strength; }

    public int getStrength() //gets the value of strength
    { return _strength; }
}
如果我在主游戏中创建一个角色变量,并使用代码为强度赋值:

public static void main(String[] args)
{
    Character character = new Character(); //initializes the Character variable

    character.setStrength(5); //sets the character's strength to 5
    System.out.println(character.getStrength()); //prints out 5 as expected.
}
如果我转到另一个没有主函数的类,例如:

public class SaveSystem
{
    private Character character = new Character(); //exactly the same as above...

    public SaveSystem()
    {  }

    public void saveGame()
    {
        //just to test the methods used earlier
        System.out.println(character.getStrength()));
    }
}
我应该能够回到这个类,带着主函数说:

public static void main(String[] args)
{
    Character character = new Character(); //initializes the Character variable
    SaveSystem save = new SaveSystem();

    character.setStrength(5); //sets the character's strength to 5
    save.saveGame(); //containing nothing but the character.getStrength() method
}
并让它打印相同的值5。但是,它打印出在字符类中初始化强度变量时指定的值0。如果更改角色类中强度的默认值,如图所示:

public class Character
{ private int _strength = 5; //initialize the strength variable }

然后主类中的save.saveGame方法将输出5。这件事我已经坚持了好几天了,尽管我做出了努力,但谷歌一点帮助都没有。

你的问题是,你在创建保存对象时正在创建一个新角色,而不是传递要保存的角色。您可以尝试以下方法:

public class SaveSystem
{
    public SaveSystem()
    {  }

    public void saveGame(Character character)
    {
        //just to test the methods used earlier
        System.out.println(character.getStrength()));
    }
}
那么你可以这样称呼它:

public static void main(String[] args)
{
    Character character = new Character(); //initializes the Character variable
    SaveSystem save = new SaveSystem();

    character.setStrength(5); //sets the character's strength to 5
    save.saveGame(character); //containing nothing but the character.getStrength() method
}

您的
保存系统
应该保存现有的
字符
对象,而不是自己创建全新的对象并保存它们

因此,请删除
保存系统中的创建内容
,并将
字符
传递给save方法

public class SaveSystem
{


    public SaveSystem()
    {  }

    public void saveGame(Character character)
    {
        //just to test the methods used earlier
        System.out.println(character.getStrength()));
    }
}


你能发一封信吗?你们都是对的。不过,我不确定在这里哪一个是正确的。非常感谢你的帮助!我在这件事上耽搁了一段时间!
public static void main(String[] args)
{
    Character character = new Character(); //initializes the Character variable
    SaveSystem save = new SaveSystem();

    character.setStrength(5); //sets the character's strength to 5
    save.saveGame(character); 
}