我想知道两件事:
- 是否值得从 switch 转换为其他东西
- 在我的例子中会是什么样子?对我来说最大的问题是“案例 ID_BOTH:”
我的小菜一碟:
public void init( int boxID ) {
initComponentText();
switch ( boxID ) {
case ID_IMAGE:
initComponentImg();
break;
case ID_BOOL:
initComponentBool();
break;
case ID_BOTH:
initComponentBool();
initComponentImg();
break;
}
}
private void initComponentImg() {
img = new ComponentImg( switchComponent );
}
private void initComponentBool() {
bool = new ComponentBool( switchComponent );
}
private void initComponentText() {
text = new ComponentText( switchComponent );
}
感谢您的帮助和提示。
最佳答案
我认为if条件对于降低代码复杂度会更有帮助;
if(ID_IMAGE==boxID||ID_BOTH==boxID)
initComponentImg();
if(ID_BOOL==boxID||ID_BOTH==boxID)
initComponentBool();
关于java - 切换到 Map/Enum 或其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418278/