以下两个switch/case语句的更好实践是什么?
有没有更简单的方法(更少的代码)来做到这一点?
switch (myValue) { case 1: { methodFor1(); break; } case 2: case 3: { methodFor2or3(); if (myValue == 2) methodFor2(); if (myValue == 3) methodFor3(); break; } } ...or... switch (myValue) { case 1: { methodFor1(); break; } case 2: case 3: { methodFor2or3(); switch (myValue) { case 2: { methodFor2(); break; } case 3: { methodFor3(); break; } } break; } }
Tomalak.. 8
switch (myValue) { case 1: methodFor1(); break; case 2: methodFor2or3(); methodFor2(); break; case 3: methodFor2or3(); methodFor3(); break; }
为什么所有的麻烦只是避免重复methodFor2or3()
一次?
switch (myValue) { case 1: methodFor1(); break; case 2: methodFor2or3(); methodFor2(); break; case 3: methodFor2or3(); methodFor3(); break; }
为什么所有的麻烦只是避免重复methodFor2or3()
一次?