[英]Having a hard time understanding enum “instantiation”?
So I have these classes that I can't change that I need to use in order to send serialized objects across to a server. 因此,我有一些我无法更改的类,这些类用于将序列化的对象发送到服务器。 But I don't have much experience with enums and am having a hard time understanding how to do this?
但是我在枚举方面没有太多经验,并且很难理解如何执行此操作?
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;
}
}
I mostly understand the serialization, this is for a simple tictactoe game, and i have a background thread running to take in objects. 我最了解序列化,这是一个简单的tictactoe游戏,并且我有一个运行中的线程来接收对象。 But how can I make a command to send to the server?
但是我该如何发送命令到服务器? Let's say i want to see a list of players, how do I MAKE the commandMessage object so i can send it?
假设我要查看播放器列表,如何制作commandMessage对象以便可以发送它? I think I'm missing something really simple >_<
我想我缺少了一些非常简单的内容> _ <
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();
}
You must use the constructor 您必须使用构造函数
CommandMessage newComm = new CommandMessage(CommandMessage.Command.LIST_PLAYERS);
or use one of the other enums provided 或使用提供的其他枚举之一
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.