Java 切换到映射/枚举或其他方式

Java 切换到映射/枚举或其他方式,java,dictionary,enums,switch-statement,Java,Dictionary,Enums,Switch Statement,我想知道两件事: 从开关转换到其他东西值得吗 我的情况会是什么样子?对我来说,最大的问题是“案例ID\u都是:” 小菜一碟: public void init( int boxID ) { initComponentText(); switch ( boxID ) { case ID_IMAGE: initComponentImg(); break; case ID_BOOL:

我想知道两件事:

  • 从开关转换到其他东西值得吗
  • 我的情况会是什么样子?对我来说,最大的问题是“案例ID\u都是:”
  • 小菜一碟:

        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();
    

    我认为if条件更有助于降低代码复杂度

        if(ID_IMAGE==boxID||ID_BOTH==boxID)
            initComponentImg();
        if(ID_BOOL==boxID||ID_BOTH==boxID)
            initComponentBool();
    

    假设您让
    ID\u两者都是
    ID\u BOOL
    ID\u IMAGE
    的位或,并且您的单个“类型”没有重叠的二进制值(例如2的幂),您可以按位和
    boxId
    检查个性。使用这种方法,您可以继续按位或将所有类型放在一起

    int ID_NONE = 0
    int ID_BOOL = 1;
    int ID_IMAGE = 2;
    int ID_TEXT = 4;
    
    int ID_BOOL_IMG = ID_BOOL | ID_IMAGE; // 3
    int ID_BOOL_TEXT = ID_BOOL | ID_TEXT; // 5
    int ID_BOOL_ALL = ID_BOOL | ID_IMAGE | ID_TEXT; // 7
    
    if ((boxId & ID_BOOL) == ID_BOOL) {
        initComponentBool(); // runs for boxId = 1, 3, 7
    }
    if ((boxId & ID_IMAGE) == ID_IMAGE) {
        initComponentImg(); // runs for boxId = 2, 3, 7
    }
    if ((boxId & ID_TEXT) == ID_TEXT) {
       initComponentText(); // runs for boxId = 4, 5, 7
    }
    

    假设您让
    ID\u两者都是
    ID\u BOOL
    ID\u IMAGE
    的位或,并且您的单个“类型”没有重叠的二进制值(例如2的幂),您可以按位和
    boxId
    检查个性。使用这种方法,您可以继续按位或将所有类型放在一起

    int ID_NONE = 0
    int ID_BOOL = 1;
    int ID_IMAGE = 2;
    int ID_TEXT = 4;
    
    int ID_BOOL_IMG = ID_BOOL | ID_IMAGE; // 3
    int ID_BOOL_TEXT = ID_BOOL | ID_TEXT; // 5
    int ID_BOOL_ALL = ID_BOOL | ID_IMAGE | ID_TEXT; // 7
    
    if ((boxId & ID_BOOL) == ID_BOOL) {
        initComponentBool(); // runs for boxId = 1, 3, 7
    }
    if ((boxId & ID_IMAGE) == ID_IMAGE) {
        initComponentImg(); // runs for boxId = 2, 3, 7
    }
    if ((boxId & ID_TEXT) == ID_TEXT) {
       initComponentText(); // runs for boxId = 4, 5, 7
    }
    
    您可以改用
    运算符

    public void init( int boxID ) {
      initComponentText();
    
      if ((boxID & ID_IMAGE) == ID_IMAGE) initComponentImg();
      if ((boxID & ID_BOOL) == ID_BOOL) initComponentBool();
    }
    
    假设

    int ID_IMAGE = 1;
    int ID_BOOL = 2;
    int ID_BOTH = 3;
    
    请参见您可以改用
    运算符

    public void init( int boxID ) {
      initComponentText();
    
      if ((boxID & ID_IMAGE) == ID_IMAGE) initComponentImg();
      if ((boxID & ID_BOOL) == ID_BOOL) initComponentBool();
    }
    
    假设

    int ID_IMAGE = 1;
    int ID_BOOL = 2;
    int ID_BOTH = 3;
    

    请参见

    在我的情况下,这将是什么样子?你的情况如何?我的情况如何?在您的情况下会是什么样子?如果boxID=ID\u这两个都不能处理呢。您是否正在考虑将boxID更改为按位或值?我猜您是对的:)我的想法是逻辑性的,而不是编程性的。答案用按位更新。谢谢你的想法@AndyThomas@Shivam-如果
    添加
    else,则只会导致输入第一个匹配条件。如果boxID=ID\u两者,该怎么办?这不会处理ID\u两者。您是否正在考虑将boxID更改为按位或值?我猜您是对的:)我的想法是逻辑性的,而不是编程性的。答案用按位更新。谢谢你的想法@AndyThomas@Shivam-如果添加
    else if
    ,则只会导致输入第一个匹配条件。