从外部调用枚举方法

发布于 2024-11-08 19:17:34 字数 1469 浏览 0 评论 0原文

代码是

public interface Command {
     public Command[] parseCommand(String command);
}

public enum CameraCommand implements Command {
    CLICK;  
    public Command[] parseCommand(String commands) {
    if ("CLICK".equals(commands))
        return new Command[] { CameraCommand.CLICK };
        return null;
    }
}

public enum RoverCommand implements Command {

    L,
    R,
    M;

public Command[] parseCommand(String commandString) {
    RoverCommand[] commands = new RoverCommand[commandString.length()];

    for (int i = 0; i < commandString.length(); i++) {
        switch (commandString.charAt(i)) {
        case 'L':
            commands[i] = RoverCommand.L;
            break;
        case 'R':
            commands[i] = RoverCommand.R;
            break;
        case 'M':
            commands[i] = RoverCommand.M;
            break;
        default:
            break;
        }
    }
    return commands;
}
}

我这样做是为了对命令类型进行分组。 现在的问题是,我得到了字符串中特定类型的命令,例如“CLICK”。我不知道类型,但我希望这样做,

Machine machine = getMachine(type); //machine is an interface, i pass type and get a typeof machine
//machine.parseCommand(commandString); //i don wish to have this logic inside the machine
Command[] command = Command.parseCommand(commandString); // this didnt work, how to solve this, work around?
machine.execute(command); ///finally pass the command and execute the action

任何帮助将不胜感激,

谢谢 V

the code is

public interface Command {
     public Command[] parseCommand(String command);
}

public enum CameraCommand implements Command {
    CLICK;  
    public Command[] parseCommand(String commands) {
    if ("CLICK".equals(commands))
        return new Command[] { CameraCommand.CLICK };
        return null;
    }
}

public enum RoverCommand implements Command {

    L,
    R,
    M;

public Command[] parseCommand(String commandString) {
    RoverCommand[] commands = new RoverCommand[commandString.length()];

    for (int i = 0; i < commandString.length(); i++) {
        switch (commandString.charAt(i)) {
        case 'L':
            commands[i] = RoverCommand.L;
            break;
        case 'R':
            commands[i] = RoverCommand.R;
            break;
        case 'M':
            commands[i] = RoverCommand.M;
            break;
        default:
            break;
        }
    }
    return commands;
}
}

I did this to group the commands type.
now the problem is, I get a command of particular type in string, ex "CLICK". I do not know the type, but I wish to do like this

Machine machine = getMachine(type); //machine is an interface, i pass type and get a typeof machine
//machine.parseCommand(commandString); //i don wish to have this logic inside the machine
Command[] command = Command.parseCommand(commandString); // this didnt work, how to solve this, work around?
machine.execute(command); ///finally pass the command and execute the action

any help would be appreciated

thanks
V

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-11-15 19:17:34

您可以像RoverCommand.L.parseCommand那样执行,因为您需要一个Command接口的实例来调用方法。
但您应该考虑将 parseCommand 设为静态方法。例如,在 RoverCommand 中将其设为静态并调用 RoverCommand.parseCommand。

我认为你应该用空格等分隔 commandString 中的所有命令。然后使用一个静态方法解析由空格分隔的每个命令,该方法决定它是“CLICK”、“L”、“R”还是“M”。
例如

String commandsString = "L CLICK R L CLICK";
List<Command> commands = CommandParser.parseCommands(commandsString);

public class CommandParser {
    public static Command parseSingleCommand(String command) {
        if ("CLICK".equals(command)) { return CameraCommand.CLICK; }
        else if ("R".equals(command)) { return RoverCommand.R; }
        else if ("L".equals(command)) { return RoverCommand.L; }
        else if ("M".equals(command)) { return RoverCommand.M; }
        else { throw new IllegalArgumentException("Unknown command: " + command); }
    }

    public static List<Command> parseCommands(String commandsString) {
       String[] commands = commandsString.split(" ");
       List<Command> result = new ArrayList<Command>();
       for (String command : commands) {
          result.add(CommandParser.parseSingleCommand(command));
       }
       return result;
    }
}

You can do like RoverCommand.L.parseCommand, because you need an instance of Command interfrace to call method.
But you should consider making parseCommand static method. For example in RoverCommand make it static and call RoverCommand.parseCommand.

I think you should spearate all commands in commandString by for example spaces. And then parse every single command, delimited by spaces, using one static method, which decides if it is "CLICK" or "L" or "R" or "M".
E.g.

String commandsString = "L CLICK R L CLICK";
List<Command> commands = CommandParser.parseCommands(commandsString);

public class CommandParser {
    public static Command parseSingleCommand(String command) {
        if ("CLICK".equals(command)) { return CameraCommand.CLICK; }
        else if ("R".equals(command)) { return RoverCommand.R; }
        else if ("L".equals(command)) { return RoverCommand.L; }
        else if ("M".equals(command)) { return RoverCommand.M; }
        else { throw new IllegalArgumentException("Unknown command: " + command); }
    }

    public static List<Command> parseCommands(String commandsString) {
       String[] commands = commandsString.split(" ");
       List<Command> result = new ArrayList<Command>();
       for (String command : commands) {
          result.add(CommandParser.parseSingleCommand(command));
       }
       return result;
    }
}
戏舞 2024-11-15 19:17:34

您的问题是您是在班级级别而不是实例级别进行操作。要解决这个问题,您应该将方法 parseCommand 声明为静态。

public static Command[] parseCommand(String commandString) {
    RoverCommand[] commands = new RoverCommand[commandString.length()];

    for (int i = 0; i < commandString.length(); i++) {
        switch (commandString.charAt(i)) {
        case 'L':
            commands[i] = RoverCommand.L;
            break;
        case 'R':
            commands[i] = RoverCommand.R;
            break;
        case 'M':
            commands[i] = RoverCommand.M;
            break;
        default:
            break;
        }
    }
    return commands;
}

Your problem is that you are acting on class level not instace level. TO solve this you should declare your method parseCommand as static.

public static Command[] parseCommand(String commandString) {
    RoverCommand[] commands = new RoverCommand[commandString.length()];

    for (int i = 0; i < commandString.length(); i++) {
        switch (commandString.charAt(i)) {
        case 'L':
            commands[i] = RoverCommand.L;
            break;
        case 'R':
            commands[i] = RoverCommand.R;
            break;
        case 'M':
            commands[i] = RoverCommand.M;
            break;
        default:
            break;
        }
    }
    return commands;
}
放飞的风筝 2024-11-15 19:17:34

我认为您有很多不需要的样板代码。如果您这样做,您可以添加命令而无需添加代码。

public interface Command {
}

public enum Commands {
    ;

    public static Command[] parseCommand(String command) {
        for (Class type : new Class[]{CameraCommand.class, RoverCommand.class})
            try {
                return new Command[]{(Command) Enum.valueOf(type, command)};
            } catch (IllegalArgumentException ignored) {
            }
        throw new IllegalArgumentException("Unknown Command " + command);
    }
}

public enum CameraCommand implements Command {
    CLICK
}

public enum RoverCommand implements Command {
    L, R, M;
}

I think you have alot of boiler plate code you don't need. If you do it this way you can add Commands without having to add code as well.

public interface Command {
}

public enum Commands {
    ;

    public static Command[] parseCommand(String command) {
        for (Class type : new Class[]{CameraCommand.class, RoverCommand.class})
            try {
                return new Command[]{(Command) Enum.valueOf(type, command)};
            } catch (IllegalArgumentException ignored) {
            }
        throw new IllegalArgumentException("Unknown Command " + command);
    }
}

public enum CameraCommand implements Command {
    CLICK
}

public enum RoverCommand implements Command {
    L, R, M;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文