Java Switch Statement

后端 未结 8 2231
南笙
南笙 2021-02-07 13:48

I have a problem using switch statement when I tried to deal with a special situation. For example, I have 3 cases: A, B, C.

  • for A, I want to do statement_1 and st
8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 14:15

    I often find introducing enums adds clarity. Here I imagine each enum is an issue which can be resolved through a number of processes:

    enum Issue {
      A {
        void handleIt () {
          statement_1();
          statement_3();
        }
      },
      B {
        void handleIt () {
          statement_2();
          statement_3();
        }
      },
      C {
        void handleIt () {
          // Do nothing.
        }
      },
      D {
        void handleIt () {
          A.handleIt();
          B.handleIt();
        }
      };
    
      abstract void handleIt();
    }
    

    Note here that you get the added benefit of being able to handle certain issues using the solutions of other issues (see my D enum).

提交回复
热议问题