在我们的学期项目中,我们负责制作桌面游戏的桌面版本。我的角色是实现保存和加载方法。为此,我正在致力于连载。我正在尝试序列化包含其他对象的对象。为了形象化,我准备了一些示例类。
import java.io.Serializable;
public class Player implements Serializable
{
static int id;
static String name;
public Player(String name)
{
this.name = name;
}
}
import java.io.Serializable;
public class Team implements Serializable
{
String name;
transient Player c;
int[] numbers;
public Team(String name, String capt){
this.name = name;
this.c = new Player(capt);
}
public Player getC()
{
return c;
}
public String getName()
{
return name;
}
}
public class test implements Serializable
{
public static void main(String args[])throws IOException
{
Team t1 = new Team("Juventus", "Ronaldo");
Team t2 = new Team("Barcelona", "Messi");
File file = createSave("1");
save(file,t1,t2);
Team[] teams = load(file);
System.out.println("Team 1 is: " + teams[0].getName());
System.out.println("Team 2 is: " + teams[1].getName());
System.out.println("Captain of team 1 is: " + teams[0].getC().name);
System.out.println("Captain of team 2 is: " + teams[1].getC().name);
}
private static File createSave(String gameId) throws IOException
{
File file = new File(gameId + ".txt");
if (file.createNewFile())
{
System.out.println("File is created!");
}
else
{
System.out.println("File already exists.");
}
return file;
}
private static void save(File file, Team t1, Team t2)throws IOException
{
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(t1);
o.writeObject(t2);
o.close();
f.close();
}
private static Team[] load(File file)throws IOException
{
try {
Team[] teams = new Team[2];
FileInputStream fi = new FileInputStream(file);
ObjectInputStream oi = new ObjectInputStream(fi);
Team team1 = (Team) oi.readObject();
Team team2 = (Team) oi.readObject();
teams[0] = team1;
teams[1] = team2;
oi.close();
fi.close();
return teams;
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
总结代码部分:我创建了一个玩家类和一个包含玩家对象的团队类。在团队类中,有一个方法返回团队的队长。我创建了两个球队实例,第一个是尤文图斯,队长是罗纳尔多。第二个是巴萨,队长是梅西。然后我使用 ObjectOutputStream 将这两个团队对象存储到一个 txt 文件中。在 load 方法中,我使用 ObjectInputStream 加载了两个团队并将它们作为数组返回。
当我尝试访问两个团队对象的名称变量时,我可以。然而,我无法联系到各队的队长。只有最后创建的玩家对象可用。让我分享我的输出:
文件已存在。
第 1 队是:尤文图斯
第 2 队是:巴塞罗那
第1队队长是:梅西
第2队队长是:梅西
如输出所示,我只能到达最后创建的内部对象。球员罗纳尔多现在迷失了。 如果您能帮助我,我将非常高兴。
最佳答案
首先,您需要练习并了解有关 java 基础知识的更多信息。 你所做的错误是在 Player 类中。您将名称设置为静态字段。所有玩家只能有 1 个名字。
尝试像这样替换你的类:
import java.io.Serializable;
public class Player implements Serializable {
private int id;
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
接下来在 Team 类中只需删除 transient
Player c;
并在主类中使用
System.out.println("Captain of team 1 is: " + teams[0].getC().getName());
System.out.println("Captain of team 2 is: " + teams[1].getC().getName());
现在您的代码应该可以工作了:)它并不完美,但这应该可以解决您的问题
关于java - 如何序列化包含java中对象的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489045/