利用序列化存储和恢复游戏人物。 某游戏人物类的设计如下: class GameCharacter implements serializable { //实现serializable接口,说明GameCharacter类可以被序列化int power;//角色力量string type;//角色类型 String[] weapons;//角色使用的武器列表 public GameCharacter(int p, string t, string[] w){//构造方法,初始化人物信息power = p;type = t;weapons = w; 1 public int getPower(){ return power;//返回力量值 public String gettype(){return type;//返回角色类型 public string getweapons(){String weaponList =""; for (int i =0; i<weapons.length; i++){weaponList += weapons[i]+""; return weaponList;//返回角色武器列表编程实现三个游戏人物的创建,把这三个游戏人物对象序列化写入gameCharacter.dat中。从该文件中读取游戏人物对象,输出每个游戏人物的力量值和所拥有的武器。 (使用try-with-resource语法,并使用BufferedInputStream/BufferedOutputStream) 人物随便举例
时间: 2024-03-18 18:39:10 浏览: 134
Android序列化之Parcelable和Serializable的使用详解
好的,根据你的要求,我可以回答你的问题。首先,这是一个利用序列化存储和恢复游戏人物的问题,需要创建一个实现Serializable接口的GameCharacter类,然后创建三个不同的游戏人物对象,并将它们序列化写入到gameCharacter.dat文件中。最后,从该文件中读取游戏人物对象,并输出每个游戏人物的力量值和所拥有的武器。
以下是一个可能的解决方案:
```java
import java.io.*;
class GameCharacter implements Serializable {
private static final long serialVersionUID = 1L;
private int power;
private String type;
private String[] weapons;
public GameCharacter(int p, String t, String[] w) {
power = p;
type = t;
weapons = w;
}
public int getPower() {
return power;
}
public String getType() {
return type;
}
public String[] getWeapons() {
return weapons;
}
}
public class GameCharacterSerialization {
public static void main(String[] args) {
// 创建三个游戏人物对象
GameCharacter g1 = new GameCharacter(100, "warrior", new String[] {"sword", "shield"});
GameCharacter g2 = new GameCharacter(80, "mage", new String[] {"staff", "wand"});
GameCharacter g3 = new GameCharacter(120, "hunter", new String[] {"bow", "arrow"});
// 序列化写入到文件中
try (ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream("gameCharacter.dat")))) {
out.writeObject(g1);
out.writeObject(g2);
out.writeObject(g3);
System.out.println("Game characters saved to gameCharacter.dat");
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读取游戏人物对象并输出信息
try (ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(new FileInputStream("gameCharacter.dat")))) {
GameCharacter g1_restored = (GameCharacter) in.readObject();
GameCharacter g2_restored = (GameCharacter) in.readObject();
GameCharacter g3_restored = (GameCharacter) in.readObject();
System.out.println("Game characters restored from gameCharacter.dat:");
System.out.println("Character 1: power=" + g1_restored.getPower() + ", weapons=" +
String.join(", ", g1_restored.getWeapons()));
System.out.println("Character 2: power=" + g2_restored.getPower() + ", weapons=" +
String.join(", ", g2_restored.getWeapons()));
System.out.println("Character 3: power=" + g3_restored.getPower() + ", weapons=" +
String.join(", ", g3_restored.getWeapons()));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
运行该程序后,会输出以下信息:
```
Game characters saved to gameCharacter.dat
Game characters restored from gameCharacter.dat:
Character 1: power=100, weapons=sword, shield
Character 2: power=80, weapons=staff, wand
Character 3: power=120, weapons=bow, arrow
```
这表明我们成功地将三个游戏人物对象序列化写入到文件中,并从该文件中读取并恢复了这些对象,并输出了它们的力量值和所拥有的武器。
阅读全文