【发布时间】:2010-11-16 04:24:31
【问题描述】:
我正在实现一些使用 switch 语句来区分不同情况的方法:
private void doThis(){
switch(command){
case on: {status = doCalculationsA; break;}
case off: {status = doCalculationsB; break;}
case idle: {status = doCalculationsC; break;}
case stdby:{status = doCalculationsD; break;}
}
}
上面的工作正常,当进一步向下业务逻辑时,我在其他需要 doThis() 功能的方法中调用 doThis()。
但是,此时我有点困惑如何结合上述开关的每种情况下的其他条件/限制。
我的意思是,当我在 bigOperation() 中调用 doThis() 时,我有新的条件需要应用于 doThis() 函数中的每个 switch 情况:
示例逻辑:
biggerOperation(){
doThat();
doTheOther();
if(somethingIsTrue){
execute "case: on" of doThis()
}
else if(somethingElseIsTrue){
execute "case: off" of doThis()
}
else if(aThirdThingIsTrue){
execute "case: idle" of doThis()
}
else if(aFourthThingIsTrue){
execute "case: stdby" of doThis()
}
}
到目前为止,我还没有想出一种优雅、干净和紧凑的方式来做到这一点。有什么想法吗?我怎样才能明确地针对开关的每种情况?我可以使用新开关来实现该条件逻辑吗?
欢迎提出任何建议。非常感谢您的帮助。
【问题讨论】:
标签: java conditional switch-statement