保存游戏数据 - Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28948315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save Game data - Java
提问by Joseph Erickson
I've created a rudimentary game with a few objects. I'd like to give the user of the program the ability to save and load the state of the program.
我用几个对象创建了一个基本的游戏。我想让程序的用户能够保存和加载程序的状态。
I've researched multiple articles and read through a lot of overstack posts. I was surprised by the amount of methods, and the complexity of each of those methods.
我研究了多篇文章并阅读了大量的文章。我对方法的数量以及每种方法的复杂性感到惊讶。
Most methods require that I create the framework or skeleton of all objects that I want to save, and then load them line by line, calling each object manually, and then casting it back into its data type and class.
大多数方法要求我创建要保存的所有对象的框架或骨架,然后逐行加载它们,手动调用每个对象,然后将其转换回其数据类型和类。
Yes, I'm new to programming. But I'm not asking for a handout. I'm asking for a concise explanation.
是的,我是编程新手。但我不是要求施舍。我要求一个简洁的解释。
I read the below article, as I explored the idea of outputting to an XML file.
我阅读了下面的文章,因为我探索了输出到 XML 文件的想法。
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
As I stated, I'm new to programming. Should I be learning XML in tandem with Java?
正如我所说,我是编程新手。我应该同时学习 XML 和 Java 吗?
My question is the following:
我的问题如下:
A) Doesn't my IDE, upon successful compilation, know how many objects I have, their data type, and what class they belong to?
A) 成功编译后,我的 IDE 不知道我有多少个对象、它们的数据类型以及它们属于哪个类?
B) Is there a way to save ALL objects without specifying them individually.
B) 有没有办法保存所有对象而不单独指定它们。
c) Have I overdramitized the complexity of saving a simple program?
c) 我是否过度夸大了保存一个简单程序的复杂性?
采纳答案by Radiodef
Should I be learning XML in tandem with Java?
我应该同时学习 XML 和 Java 吗?
There's no reason to unless you want to learn XML or think XML is a good option for your application. There are many ways to serialize without XML.
除非您想学习 XML 或认为 XML 是您的应用程序的不错选择,否则没有理由这样做。有很多方法可以在没有 XML 的情况下进行序列化。
Doesn't my IDE, upon succesful compilation, know how many objects I have, their data type, and what class they belong to?
成功编译后,我的 IDE 不知道我有多少个对象、它们的数据类型以及它们属于哪个类?
No, it doesn't have to. Since allocations are done dynamically in Java, there are situations where it's even impossible to know this information statically.
不,没有必要。由于分配是在 Java 中动态完成的,因此在某些情况下甚至不可能静态地知道这些信息。
Foo[] foos = new Foo[ new Scanner(System.in).nextInt() ];
for(int i = 0; i < foos.length; ++i)
foos[i] = new Foo();
(How many Foo
s were created? The compiler doesn't know.)
(Foo
创建了多少个?编译器不知道。)
Certainly the compiler knows a lot about the typesof data.
当然,编译器非常了解数据类型。
B) Is there a way to save ALL objects without specifying them individually.
B) 有没有办法保存所有对象而不单独指定它们。
Maybe with Serializable. Basically you will put all your program state inside a single object. When you serialize that single object, everything else gets serialized recursively.
也许与可序列化。基本上,您会将所有程序状态放在一个对象中。当您序列化该单个对象时,其他所有内容都会递归序列化。
class FooState implements Serializable {
Integer a, b, c; // Integer and String
String d, e, f; // also implement Serializable
}
class Foo {
static FooState allMyState = new FooState();
public static void main(String[] args) throws IOException {
try(ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(new File("myFile.data")))) {
// no need to specify members individually
oos.writeObject(allMyState);
}
}
}
The same concept probably applies to other serialization schemes like JSON, XML, etc.
相同的概念可能适用于其他序列化方案,如 JSON、XML 等。
You need to think carefully about what you put in the state object since everything in it will get saved.
您需要仔细考虑在状态对象中放入的内容,因为其中的所有内容都将被保存。
c) Have I overdramitized the complexity of saving a simple program?
c) 我是否过度夸大了保存一个简单程序的复杂性?
Not really. Saving program state can be complicated if it's not just a few values.
并不真地。如果不是几个值,保存程序状态可能会很复杂。
回答by Jaroslaw Pawlak
A) Doesn't my IDE, upon succesful compilation, know how many objects I have, their data type, and what class they belong to?
A) 成功编译后,我的 IDE 不知道我有多少个对象、它们的数据类型以及它们属于哪个类?
Loading the state of the program has nothing to do with IDE or compilation. You start your program and it's already running, you want to change it's state by loading some state from the file.
加载程序的状态与IDE或编译无关。你启动你的程序并且它已经在运行,你想通过从文件加载一些状态来改变它的状态。
If you use serialization, you may face problems related to versions and classes incompatibility - e.g. if you decide to add a new field to a class, you will need to write extra code to be able to load an older state, which didn't have that field. So other format such as XML, JSON or plain text (and doing the conversion yourself) may be better idea.
如果您使用序列化,您可能会面临与版本和类不兼容相关的问题——例如,如果您决定向类添加一个新字段,您将需要编写额外的代码才能加载旧状态,而这些旧状态没有那个领域。所以其他格式,如 XML、JSON 或纯文本(并自己进行转换)可能是更好的主意。
B) Is there a way to save ALL objects without specifying them individually.
B) 有没有办法保存所有对象而不单独指定它们。
Well, theoretically it should be possible to save the state of entire JVM and load it back later. It's an advanced topic and there would be a lot of problems because of all clever stuff going on inside JVM (such as Just-In-Time compiler, garbage collection and various on-the-fly optimizations). I don't recommend going down this way. Most probably, you won't be able to change your code at all - e.g. if you add new button in your GUI, the loaded version will not have it, because it will be loading old versions of the classes. So it won't be possible to load an old game state into new version of the game.
嗯,理论上应该可以保存整个 JVM 的状态,然后再加载回来。这是一个高级主题,由于 JVM 内部发生的所有聪明的事情(例如即时编译器、垃圾收集和各种动态优化),会出现很多问题。我不建议这样下去。最有可能的是,您根本无法更改代码——例如,如果您在 GUI 中添加新按钮,则加载的版本将没有它,因为它将加载旧版本的类。因此,不可能将旧游戏状态加载到新版本的游戏中。
C) Have I overdramitized the complexity of saving a simple program?
C) 我是否夸大了保存一个简单程序的复杂性?
Let's say that your game has class called Game
with constructor Game(String name)
(and some other parameters). For the first time, you can create it by just calling new Game("firstGame")
. But now you want to create this object from some saved state. You can either serialize the object and then load it back, the result will be the object itself. Or you can read the file to get that string and just call the constructor yourself using this string. Going further this way there are libraries that can convert object to JSON and JSON to object (such as Hymanson serializer).
假设您的游戏Game
使用构造函数Game(String name)
(和一些其他参数)调用了类。第一次,您只需调用 即可创建它new Game("firstGame")
。但是现在您想从某个保存的状态创建这个对象。您可以序列化对象然后将其加载回,结果将是对象本身。或者您可以读取文件以获取该字符串,然后使用该字符串自己调用构造函数。更进一步,有一些库可以将对象转换为 JSON 和 JSON 到对象(例如 Hymanson 序列化程序)。
回答by prasad vsv
Have a class that can save the current state of the game.
有一个可以保存游戏当前状态的类。
public class Game{
int coOrdinateX;
int coOrdinateY;
int score;
String[] powersCurrentlyTheUserHad;
...
}
when ever user exits, load this class with relevant data. As once the game terminates, memory vanishes, you need to save this in a file.
当用户退出时,用相关数据加载这个类。一旦游戏终止,内存就会消失,您需要将其保存在一个文件中。
Serialization is a way of storing Objects to disk(essentially into files), such that, when you de-serialize using the library functions, you directly get back the object with the current state.
序列化是一种将对象存储到磁盘(本质上是存储到文件中)的方式,这样,当您使用库函数反序列化时,您可以直接取回具有当前状态的对象。
check http://www.tutorialspoint.com/java/java_serialization.htmfor more details
查看 http://www.tutorialspoint.com/java/java_serialization.htm了解更多详情