如何在switch语句中执行所有情况而又不重复大型“多合一”情况下的现有情况?
例如
switch(obj.nObjType)
{
case eElephant:
...
break;
case eFoo:
...
break;
case eSeptember:
...
break;
default:
return;
}
我想强制执行所有3种情况(eElephant, eFoo, eSeptember
)的代码break;
,例如,没有nObjType = eAllTypes
。
然后使用if
语句:
if (obj.nObjType == eElephant || obj.nObjType == eAllTypes)
{
// Elephant code
}
if (obj.nObjType == eFoo || obj.nObjType == eAllTypes)
{
// Foo code
}
// etc.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句