繁体   English   中英

难以理解枚举“实例化”吗?

[英]Having a hard time understanding enum “instantiation”?

因此,我有一些我无法更改的类,这些类用于将序列化的对象发送到服务器。 但是我在枚举方面没有太多经验,并且很难理解如何执行此操作?

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();       
}

您必须使用构造函数

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

或使用提供的其他枚举之一

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM