序列化包含其他对象列表的对象

时间:2015-06-10 13:25:15

标签: java sockets serialization

背景:我正在实施一个需要序列化模型对象(事件)的游戏,以便能够通过网络将它们发送给玩游戏的客户。

问题:我的一个模型类是EventShowCards,其子类Event(声明为可序列化)。

public class EventShowCards extends Event {
    private final Player player;
    private final List<ItemCard> cards;

    public EventShowCards(Player player, List<ItemCard> cards) {
        super();
        this.player = player;
        this.cards = cards;
    }

    public Player getPlayer() {
        return player;
    }

    public List<ItemCard> getCards() {
        return cards;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

但是,当我序列化对象并将其发送到客户端时,客户端会收到对象,ItemCard的列表为空。 不会抛出异常

服务器显示的事件(序列化之前):

客户端显示的事件(序列化后):

为了序列化/反序列化我正在使用标准的ObjectOutputStream / ObjectInputStream

任何人都知道这里会发生什么?感谢。

修改

作为请求,我发布了ItemCard(和卡)的代码:

ItemCard.java

public abstract class ItemCard extends Card {

    /**
     * By default a card ca be played independently from the game state, subclasses that have more complicated rules
     * must override this method.
     */
    public boolean canBePlayed(GameStateTurn gameState) {
        return true;
    }

    protected void emitEventCardPlayed(Player cardPlayer, GameState gameState)  {
        notifyObservers(new EventCardPlayed(cardPlayer, this));
    }

}

Card.java

public abstract class Card extends ConcreteObservable<Event> implements CardBehavior, Serializable {
    protected Deck<? extends Card> deck; // The deck to which this card belongs

    public Card() {
        super();
    }

    public void setDeck(Deck<? extends Card> deck) {
        this.deck = deck;
    }

    public void discard() {
        deck.discard(this);
    }

    @Override
    public void preEffect(Player cardPlayer, GameState gameState) {
        // By default cards have no preEffects
    }

    @Override
    public void postEffect(Player cardPlayer, GameState gameState) {
        // By default after a card is played it is discarded
        discard();
    }

    @Override
    public String toString() {
        String className = this.getClass().getSimpleName();
        return cardNameFromClassName(className);
    }

    private String cardNameFromClassName(String className) {
        StringBuilder sb = new StringBuilder();
        for (Character c: className.toCharArray()) {
            if (Character.isUpperCase(c)) {
                sb.append(" ").append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return "\"" + sb.substring(1) + "\"";
    }
}

编辑2

和Deck.java

public class Deck<T extends Card>
        extends ConcreteObservable<Event>
        implements Observer<Event>, Serializable {...}

编辑3

用于初始化EventShowCards的项目卡列表是一个已经创建的列表,来自我的应用程序的另一个模型对象,按原样传递。创建另一个ArrayList(复制该对象)以某种方式解决了我的问题。

1 个答案:

答案 0 :(得分:0)

列表可以包含无限数量的对象类型 当您拥有EventShowCards序列化(通过Event类)时,您还需要强制您的ItemCard类可序列化,以便在需要时进行编组和解组。

修改

java.util.List没有实现Serializable,这就是您的列表未被序列化的原因。 使用可序列化的其他集合,如ArrayList