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