gpt4 book ai didi

java - 切换到 Map/Enum 或其他

转载 作者:行者123 更新时间:2023-12-01 18:10:31 25 4
gpt4 key购买 nike

我想知道两件事:

  1. 是否值得从 switch 转换为其他东西
  2. 在我的例子中会是什么样子?对我来说最大的问题是“案例 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com