gpt4 book ai didi

java - 保存游戏状态

转载 作者:行者123 更新时间:2023-12-01 18:41:33 25 4
gpt4 key购买 nike

我正在制作一个仅打字的游戏(无图形),我需要保存游戏的状态并稍后重新加载。我一直在想,但什么也想不出来(我不是一个非常有经验的程序员)。有人可以启发我吗?

最佳答案

您需要您的类实现Serialized接口(interface)。然后将对象写入文件。然后在启动时再次读回该文件。

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object.

Serialzable 接口(interface)的优点在于您不需要实现任何方法。它是一个标记接口(interface)。您只需创建一个类Serialized,然后将其写入文件即可。

这里请注意:每次写入文件时都需要截断该文件。不要尝试向其中附加数据。它损坏了文件头。

教程在这里:http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

关于java - 保存游戏状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784628/

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