java case 多个_Java switch语句中的多个/重复个案

我想知道

Java如何处理同一个案例的多个相同实例.

从概念上讲,我认为以下内容是有道理的:

switch (someIntegerValue)

{

case 1:

case 2:

DoSomethingForBothCases();

break;

case 3:

DoSomethingUnrelated();

break;

case 1:

DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();

break;

case 2:

DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();

break;

}

本质上,我希望为案例1或2(使用直通)执行一大块代码,但是稍后,只有一个代码块仅针对案例2执行.

相反,以下是必要的吗?

switch (someIntegerValue)

{

case 1:

DoSomethingForBothCases();

DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();

break;

case 2:

DoSomethingForBothCases();

DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();

break;

case 3:

DoSomethingUnrelated();

break;

}

我的实际代码更复杂,但是会使用相同的原则(例如“案例1:nope;好吧……案例2:是的!执行此代码!;案例3:nope;案例1再次?:仍然没有! ;案例2再次?:是的!执行此代码;没有更多案例:全部完成!“)


版权声明:本文为weixin_29847829原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。