从外部调用枚举方法
代码是
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像
RoverCommand.L.parseCommand
那样执行,因为您需要一个Command接口的实例来调用方法。但您应该考虑将
parseCommand
设为静态方法。例如,在 RoverCommand 中将其设为静态并调用 RoverCommand.parseCommand。我认为你应该用空格等分隔
commandString
中的所有命令。然后使用一个静态方法解析由空格分隔的每个命令,该方法决定它是“CLICK”、“L”、“R”还是“M”。例如
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.
您的问题是您是在班级级别而不是实例级别进行操作。要解决这个问题,您应该将方法 parseCommand 声明为静态。
Your problem is that you are acting on class level not instace level. TO solve this you should declare your method parseCommand as static.
我认为您有很多不需要的样板代码。如果您这样做,您可以添加命令而无需添加代码。
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.