Java中嵌套Switch语句的替代方法
因此,我今天编写了一个方法,其中包含了嵌套switch语句的使用,代码在我看来相当干净简洁,但我被告知嵌套switch语句通常不是最好的方法,因为它们可能会与您添加的更多switch语句相混淆。下面是我的代码的示例:Java中嵌套Switch语句的替代方法,java,nested,switch-statement,performance,Java,Nested,Switch Statement,Performance,因此,我今天编写了一个方法,其中包含了嵌套switch语句的使用,代码在我看来相当干净简洁,但我被告知嵌套switch语句通常不是最好的方法,因为它们可能会与您添加的更多switch语句相混淆。下面是我的代码的示例: EnumOne enumOne; EnumTwo enumTwo = null; EnumTwo enumThree = null; switch (enumOne) { case CASE_ONE: switch (enumTwo){ ca
EnumOne enumOne;
EnumTwo enumTwo = null;
EnumTwo enumThree = null;
switch (enumOne) {
case CASE_ONE:
switch (enumTwo){
case A: enumTwo = EnumTwo.B; break;
case C: enumTwo = EnumTwo.D; break;
default: break;
}
switch (enumThree) {
case AA: enumThree = EnumTwo.BB; break;
case CC: enumThree = EnumTwo.DD; break;
default: break;
}
break;
case CASE_TWO:
case CASE_THREE:
switch(EnumTwo) {
default: break;
}
switch (enumThree) {
case AA: enumThree = EnumTwo.XX; break;
case CC: enumThree = EnumTwo.YY; break;
default: break;
}
break;
default:
break;
}
因此,我的问题是,从本质上说,什么是这些switch语句的合适替代方案?我建议您将每个嵌套switch语句替换为对过程的调用,然后执行嵌套switch代码 写下如下内容:
EnumOne enumOne;
EnumTwo enumTwo = null;
EnumTwo enumThree = null;
switch (enumOne)
{
case CASE_ONE:
nested_switch1();
case CASE_TWO:
case CASE_THREE:
nested_switch2();
break;
default:
break;
}
nested_switch1() {
switch (enumTwo)
{
case A:
enumTwo = EnumTwo.B;
break;
case C:
enumTwo = EnumTwo.D;
break;
default:
break;
}
switch (enumThree)
{
case AA:
enumTwo = EnumTwo.BB;
break;
case CC:
enumTwo = EnumTwo.DD;
break;
default:
break;
}
break;
}
nested_switch2() {
switch(EnumTwo)
{
default:
break;
}
switch (enumThree)
{
case AA:
enumTwo = EnumTwo.XX;
break;
case CC:
enumTwo = EnumTwo.YY;
break;
default:
break;
}
}
在这种情况下,我们可以考虑使用<代码>多态性>代码> < /p> 我将给你们上一堂简单的课,让你们明白。 假设前面有一个带有switch case的类
class Test
{
Animal a;
public Test(Animal a)
{
this.a=a;
}
public moveThisAnimal()
{
switch(this.a)
{
case fish:
System.out.println("swim");
break;
case dog:
System.out.println("walk");
break;
case bird:
System.out.println("fly");
break;
}
}
}
class Test
{
Animal a;
public Test(Animal a)
{
this.a=a;
}
public moveThisAnimal()
{
System.out.println(this.a.move()); // all switch case statements removed
}
}
现在我们用多态逻辑替换这些开关
Interface Animal
{
String move();
}
Class Dog implements Animal
{
public String move()
{
return "walk";
}
}
Class Bird implements Animal
{
public String move()
{
return "fly";
}
}
Class Fish implements Animal
{
public String move()
{
return "swim";
}
}
现在我们有了没有开关盒的测试类
class Test
{
Animal a;
public Test(Animal a)
{
this.a=a;
}
public moveThisAnimal()
{
switch(this.a)
{
case fish:
System.out.println("swim");
break;
case dog:
System.out.println("walk");
break;
case bird:
System.out.println("fly");
break;
}
}
}
class Test
{
Animal a;
public Test(Animal a)
{
this.a=a;
}
public moveThisAnimal()
{
System.out.println(this.a.move()); // all switch case statements removed
}
}
即使我们必须添加更多的案例,我们也必须只添加实现,这里没有变化
查看完整的代码并查看是否可以执行如果您有整数X和Y,并且需要同时启用它们,则可以以某种明确的方式组合它们并启用组合。例如,如果y<10:
switch (x*10+y)
{
case 0: // x == y == 0
case 1: // x ==0, y == 1
///
case 10: // x == 1, y == 0
case 11: // x == y == 1
//
}
你用它干什么?为状态机建模?所以enumTwo设置它的值取决于enum的1、2和3的值?为什么不在enumTwo中使用一个返回新值的方法呢?与状态模式类似。使用大量嵌套的switch/if语句通常会显示出设计缺陷,特别是在使用OO语言进行此操作时,应使用其上的可用功能。这是最简单也是最好的方法。这假设顺序并不重要。x*10+y=y*10+x。这可能导致错误的结果positives@inkalimeva这个例子做出了这样的假设。这是一个非常有用的答案,只是我发现了一些问题。“Class”应该是小写,并且覆盖每个动物的移动。另外,第一个示例将它们显示为System.out.println,但它不是以“正确”的方式显示的。我是这样做的:public void moveThisAnimal(){System.out.println(this.a.move());//删除了所有开关大小写语句}public static void main(String[]args){Dog d=new Dog();test t=new test(d);t.moveThisAnimal();}一个基本的OOP原则。