Java 列表<;列表<;盒子>&燃气轮机;未序列化
我有一个类Java 列表<;列表<;盒子>&燃气轮机;未序列化,java,serialization,deserialization,Java,Serialization,Deserialization,我有一个类Player,它有三个变量,其中一个是列表,当然类框实现了可序列化。 Box也有一些变量,它们都是基本类型,除了类Dice也实现了Serializable 我必须用一个套接字通过网络发送这个类,当我将它发送到客户端时,列表看起来正常,每个框,问题是,应该在框中的Dice类始终设置为null,即使我从服务器发送到客户端的类不是null,并且我绝对确定网络部分是正确的 忘了提到当实例化时,List会变成一个应可序列化的ArrayList Diceclass: package ingsw.m
Player
,它有三个变量,其中一个是列表
,当然类框
实现了可序列化
。
Box
也有一些变量,它们都是基本类型,除了类Dice
也实现了Serializable
我必须用一个套接字通过网络发送这个类,当我将它发送到客户端时,列表
看起来正常,每个框
,问题是,应该在框中的Dice
类始终设置为null,即使我从服务器发送到客户端的类不是null,并且我绝对确定网络部分是正确的
忘了提到当实例化时,List
会变成一个应可序列化的ArrayList
Dice
class:
package ingsw.model;
import java.io.Serializable;
import java.util.Random;
public class Dice implements Serializable {
private int faceUpValue;
private final Color diceColor;
public Dice(Color diceColor) {
this.diceColor = diceColor;
}
public Dice(int faceUpValue, Color diceColor) {
this.faceUpValue = faceUpValue;
this.diceColor = diceColor;
}
/**
* Draft the dice
* get a random number between 1 and 6 and set the faceUpValue
*/
void roll() {
int value = (new Random()).nextInt(6) + 1;
setFaceUpValue(value);
}
public int getFaceUpValue() {
return faceUpValue;
}
public void setFaceUpValue(int faceUpValue) {
this.faceUpValue = faceUpValue;
}
public Color getDiceColor() {
return diceColor;
}
@Override
public String toString() {
if (faceUpValue != 0) {
return diceColor.toString() + String.valueOf(faceUpValue);
} else {
return diceColor.toString();
}
}
}
public class Box implements Serializable {
private Color color;
private Integer value;
private Dice dice;
public Box(Color color) {
this.color = color;
}
public Box(Integer value) {
this.value = value;
}
public Color getColor() {
return color;
}
public Integer getValue() {
return value;
}
public boolean isValueSet() {
return value != null;
}
public void insertDice(Dice dice) {
this.dice = dice;
//TODO the dice at this point must removed from the dice drafted --> dices (set).remove();
}
public void removeDice() {
if (dice != null) dice = null;
//TODO dice must be re-added?
}
public Dice getDice() {
return dice;
}
@Override
public String toString() {
if (isValueSet()) return String.valueOf(value);
else return color.toString();
}
Boolean isDiceSet(){ return dice != null; }
}
卡
类实现可序列化:
public abstract class PatternCard extends Card {
private int difficulty;
protected List<List<Box>> grid;
public PatternCard(String name, int difficulty) {
super(name);
fillGrid();
this.difficulty = difficulty;
}
@Override
public String toString() {
return "PatternCard{" +
"'" + getName() + "'" +
'}';
}
public int getDifficulty() {
return difficulty;
}
public void setGrid(List<List<Box>> grid) {
this.grid = grid;
}
public List<List<Box>> getGrid() {
return grid;
}
private void fillGrid() {
this.grid = new ArrayList<>(4);
this.grid.add(new ArrayList<>(5));
this.grid.add(new ArrayList<>(5));
this.grid.add(new ArrayList<>(5));
this.grid.add(new ArrayList<>(5));
}
}
我做了一些调查,认为我应该自己序列化对象并反序列化它,但我不知道这是否是真正的问题,因为ArrayList是可序列化的,并且这些ArrayList中包含的每个对象也是可序列化的
这可能会有什么问题?如果没有看到代码,我们将不会有多大帮助。请提供一个。我已经用一些代码更新了OP,我已经详细解释了什么是可序列化的,什么不是,我不知道这会有多大帮助。是java.awt
包中的Color
吗?不,这是我创建的@Logan枚举,它确实实现了可序列化,默认情况下它们应该是可序列化的,但是当我没有想法时,我几乎在所有地方都设置了它,以确保我没有遗漏任何东西