我目前正在使用 GWT 为 NAO 机器人设计一个 Remote ,并且正在寻找通过 RPC 执行按钮的假定方法的最佳实践。目前我正在检查标题为 event.getSource() 的按钮名称。但这会导致大量的 if else 语句。我怎样才能做得又漂亮又整洁呢? ;)
最佳答案
您可以使用枚举或整数作为通过 Button 构造函数设置的标识符。然后在服务器端,您可以使用 switch case 语句保存一些代码行。
public enum MyButtons {
WALK,
SIT,
...
}
[...]
//ON CLIENT
Button whatever = new Button(MyButtons.WALK);
//ON SERVER
MyButtons action = whatever.getType();
// getType() should provide the value from the constructor
switch(action)
{
case WALK : doStuff();
case SIT : doStuff();
...
}
关于java - 为许多按钮实现 ClickHandler 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970595/