Switch statement inside a switch statement?

前端 未结 8 1816
傲寒
傲寒 2021-02-07 02:40

I have to evaluate many conditions. In my case, I have to do something like this:

switch(id)
{
    case 5:
         // switch some other cases here
    case 6:
          


        
8条回答
  •  佛祖请我去吃肉
    2021-02-07 03:14

    Better practice is to encapsulate the different behaviour polymophically inside different classes and try to avoid the switch statements if possible.

    This is not always possible, and if you must use switch statements then I would not put another nested switch statement (or a collection of if statements) but would probably have a method call which would contain that logic.

    If you post a bit more detail on what you are trying to do then we might be able to offer better suggestions.

提交回复
热议问题