考虑下面的 java 中的示例代码,它想要搜索操作中允许的任务
public boolean acceptableTaskForAction(String taskName,String actionName) {
String[] allowedActions;
switch (taskName){
case "Payment" :
allowedActions = { "full-payment", "bill-payment"};
case "Transfer" :
allowedActions = { "transfer-to-other", "tarnsfer-to-own"};
}
for (String action : allowedActions){
if (actionName.equals(action)){
return true;
}
}
return false;
}
如您所知,上述内容不会编译为Array constants can only be used in initializers
我想定义不同的参数,所以它将是
public boolean acceptableTaskForAction(String taskName,String actionName) {
String[] allowedActionsForPayment= { "full-payment", "payment"};
String[] allowedActionsForTransfer= { "transfer-to-other", "tarnsfer-to-own"};
String[] allowedActions={};
switch (taskName){
case "Payment" :
allowedActions = allowedActionsForPayment;
case "Transfer" :
allowedActions = allowedActionsForTransfer;
}
for (String action : allowedActions){
if (actionName.equals(action)){
return true;
}
}
return false;
}
你有没有想过其他的解决方案!?你认为最好的解决方案是什么?