0

したがって、シリアル化されたオブジェクトをサーバーに送信するために使用する必要がある、変更できないこれらのクラスがあります。しかし、私は列挙型の経験があまりなく、これを行う方法を理解するのに苦労していますか?

import java.io.Serializable;

public abstract class Message implements Serializable {

    private static final long serialVersionUID = 0L;

    private final MessageType type;

    public Message(MessageType type) {
        this.type = type;
    }

    public MessageType getType() {
        return type;
    }

@Override
    public String toString() {
        return type.toString();
    }
}



public final class CommandMessage extends Message {

    private static final long serialVersionUID = 0L;

    private final Command cmd;

    public CommandMessage(Command cmd) {
        super(MessageType.COMMAND);
        this.cmd = cmd;
    }

    public Command getCommand() {
        return cmd;
    }

    public static enum Command {

        LIST_PLAYERS, EXIT, SURRENDER;

        private static final long serialVersionUID = 0L;
    }
}

私はシリアライゼーションをほとんど理解しています。これは単純な tictactoe ゲーム用であり、オブジェクトを取り込むためにバックグラウンド スレッドが実行されています。しかし、どうすればサーバーに送信するコマンドを作成できますか? プレイヤーのリストを見たいとしましょう。送信できるように commandMessage オブジェクトを作成するにはどうすればよいですか? 本当に単純なものが欠けていると思います>_<

public static void main(String[]args) throws IOException{

    //make tictactoe client
    TicTacToeClient newClient = new TicTacToeClient();

    //start run
    newClient.run();    

    //start a connection, send username
    ConnectMessage connect = new ConnectMessage("User17");  
    newClient.out.writeObject(connect);

    CommandMessage newComm = new CommandMessage(); //what!? HOW?
    //s.BOARD = PLAYERLIST;???

    //NOPE.
    //PlayerListMessage playerList = new PlayerListMessage();               
    System.out.println();       
}
4

1 に答える 1

0

コンストラクターを使用する必要があります

CommandMessage newComm = new CommandMessage(CommandMessage.Command.LIST_PLAYERS);

または、提供されている他の列挙型のいずれかを使用します

于 2014-11-18T23:36:22.677 に答える