gpt4 book ai didi

java - 如何在另一个类中使用 'main' 方法中的变量?序列化

转载 作者:行者123 更新时间:2023-12-01 11:35:06 25 4
gpt4 key购买 nike

这里是新手!我正在使用 Java 中的保存/加载,但我被踩住了。我的代码正确保存和加载所有内容,但我无法让这些加载的变量在加载类中的“public static void main”之外的任何地方使用。我的意思是:

我有这个保存类,它工作得很好:

package com.mestru.esport;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

public class NewPlayer {

public static void main (String args[])
{
Random rand = new Random();

int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);

try
{
FileOutputStream saveFile = new FileOutputStream("Player.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);

save.writeObject(naturalTalent);
save.writeObject(luck);
save.writeObject(figure);
save.writeObject(agility);
save.writeObject(intelligence);
save.writeObject(multiTasking);
save.writeObject(stamina);

save.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}

然后我有一个类,该类旨在加载并使加载的变量可在其他类中使用。如果加载部分工作完美,那么我就无法在“main”方法之外使用它们。

package com.mestru.esport;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Random;

public class Player
{
int naturalTalent;
int luck;
int figure;
int agility;
int intelligence;
int multiTasking;
int stamina;

public static void main (String args[]) {

int NEWnaturalTalent = 0;
int NEWluck = 0;
int NEWfigure = 0;
int NEWagility = 0;
int NEWintelligence = 0;
int NEWmultiTasking = 0;
int NEWstamina = 0;


try {
FileInputStream saveFile = new FileInputStream("Player.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);

NEWnaturalTalent = (Integer) save.readObject();
NEWluck = (Integer) save.readObject();
NEWfigure = (Integer) save.readObject();
NEWagility = (Integer) save.readObject();
NEWintelligence = (Integer) save.readObject();
NEWmultiTasking = (Integer) save.readObject();
NEWstamina = (Integer) save.readObject();

save.close();
} catch (Exception exc) {
exc.printStackTrace();
}
Player player = new Player();

player.naturalTalent = NEWnaturalTalent;
player.luck = NEWluck;
player.figure = NEWfigure;
player.agility = NEWagility;
player.intelligence = NEWintelligence;
player.multiTasking = NEWmultiTasking;
player.stamina = NEWstamina;

}

public void test ()
{
System.out.println("Natural Talent: " + naturalTalent);
System.out.println("Luck: " + luck);
System.out.println("Figure: " + figure);
System.out.println("Agility: " + agility);
System.out.println("Intelligence: " + intelligence);
System.out.println("Multitasking: " + multiTasking);
System.out.println("Stamina: " + stamina);
}

}

当我在另一个类中使用“test”方法时,所有值都等于 0。

这是我的问题。我可以以某种方式使所有实例变量等于“main”方法中加载的变量吗?我也不能在“main”方法之外使用“try”:(

提前感谢您的帮助<3每一条线索都值得赞赏,我刚刚开始学习。

最佳答案

当你加载时,你创建一个新的Player,设置它然后丢弃它。

也许您打算设置当前播放器的字段。

而且您也不需要临时变量。您只需设置字段即可。

public static void main(String... ignored) throws IOException {
Player player = new Player();
player.load("Player.sav");
player.test();
}

public void load(String filename) throws IOException {
try (FileInputStream saveFile = new FileInputStream(filename);
ObjectInputStream save = new ObjectInputStream(saveFile)) {
naturalTalent = (Integer) save.readObject();
luck = (Integer) save.readObject();
figure = (Integer) save.readObject();
agility = (Integer) save.readObject();
intelligence = (Integer) save.readObject();
multiTasking = (Integer) save.readObject();
stamina = (Integer) save.readObject();
}
}

您可以将其写入文本,而不是以二进制形式写入此数据。这将使阅读/编辑变得更容易,并且在此用例中会更加紧凑。即序列化一个 Integer 对象可能会大得惊人。

关于java - 如何在另一个类中使用 'main' 方法中的变量?序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086741/

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