【问题标题】:Calling enum methods from outside从外部调用枚举方法
【发布时间】:2011-05-20 09:53:35
【问题描述】:

代码是

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

【问题讨论】:

  • 这里没有问题。请确保当您在 Stackoverflow 上提问时,您实际上在某处包含问号

标签: java interface enums abstraction


【解决方案1】:

你可以像RoverCommand.L.parseCommand那样做,因为你需要一个命令接口的实例来调用方法。 但是您应该考虑制作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;
    }
}

【讨论】:

    【解决方案2】:

    您的问题是您在类级别而不是实例级别上执行操作。要解决这个问题,您应该将您的方法 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;
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        • 2012-02-09
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        相关资源
        最近更新 更多