在 Java 中打开枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6391777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:33:51  来源:igfitidea点击:

Switch on Enum in Java

java

提问by user663321

Possible Duplicates:
Compilation error - switch with enum
Why do I get an Enum constant reference cannot be qualified in a case label?

可能的重复:
编译错误 - 用枚举切换
为什么我得到一个枚举常量引用不能在 case 标签中被限定?

Why can't you switch on an enum in Java? It seems simple enough to do and would make for some convenient code. Also this question could apply to String's. You can switch on a char, but not a String...?

为什么不能在 Java 中打开枚举?这看起来很简单,并且可以编写一些方便的代码。这个问题也适用于String's. 您可以打开一个char,但不能打开一个String...?

回答by CoolBeans

You definitely can switch on enums. An example posted from the Java tutorials.

你绝对可以打开枚举。从Java 教程发布的示例。

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 
}

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {

        switch (day) {
            case MONDAY: 
                System.out.println("Mondays are bad.");
                break;

            case FRIDAY: 
                System.out.println("Fridays are better.");
                break;

            case SATURDAY:
            case SUNDAY: 
                System.out.println("Weekends are best.");
                break;

            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }
}

回答by aioobe

Article on Programming.Guide: Switch on enum

关于 Programming.Guide 的文章:开启枚举



enum MyEnum { CONST_ONE, CONST_TWO }

class Test {
        public static void main(String[] args) {
            MyEnum e = MyEnum.CONST_ONE;

            switch (e) {
                case CONST_ONE: System.out.println(1); break;
                case CONST_TWO: System.out.println(2); break;
            }
        }
    }

Switches for strings are implemented in Java 7.

字符串的开关在 Java 7 中实现。

回答by Michael Lowman

First, you can switch on an enumin Java. I'm guessing you intended to say you can't, but you can. chars have a set range of values, so it's easy to compare. Strings can be anything.

首先,您可以enum在 Java 中打开。我猜你想说你不能,但你可以。chars 有一组值,因此很容易比较。字符串可以是任何东西。

A switchstatement is usually implemented as a jump table (branch table)in the underlying compilation, which is only possible with a finite set of values. C# canswitch on strings, but it causes a performance decrease because a jump table cannot be used.

一条switch语句通常在底层编译中实现为一个跳转表(分支表),这只能在有限的一组值中实现。C#可以开启字符串,但是会导致性能下降,因为不能使用跳转表。

Java 7 and later supports Stringswitcheswith the same characteristics.

Java 7 及更高版本支持具有相同特性的String开关

回答by brayne

You might be using the enums incorrectly in the switch cases. In comparison with the above example by CoolBeans.. you might be doing the following:

您可能在 switch case 中错误地使用了枚举。与 CoolBeans 的上述示例相比,您可能会执行以下操作:

switch(day) {
    case Day.MONDAY:
        // Something..
        break;
    case Day.FRIDAY:
        // Something friday
        break;
}

Make sure that you use the actual enum values instead of EnumType.EnumValue

确保您使用实际的枚举值而不是 EnumType.EnumValue

Eclipse points out this mistake though..

Eclipse 虽然指出了这个错误..

回答by Michael Bazos

Actually you can use a switch statement with Strings in Java...unfortunately this is a new feature of Java 7, and most people are not using Java 7 yet because it's so new.

实际上,您可以在 Java 中使用带有字符串的 switch 语句……不幸的是,这是 Java 7 的一个新特性,大多数人还没有使用 Java 7,因为它太新了。

回答by Greg Mattes

You actually can switchon enums, but you can't switchon Strings until Java 7. You might consider using polymorphic method dispatch with Java enums rather than an explicit switch. Note that enums are objects in Java, not just symbols for ints like they are in C/C++. You can have a method on an enumtype, then instead of writing a switch, just call the method - one line of code: done!

您实际上可以switchenums 上,但switchStringJava 7 之前您不能在s 上。您可能会考虑对 Java enums使用多态方法调度而不是显式的switch。请注意,enums 是 Java 中的对象,而不仅仅是ints 的符号,就像它们在 C/C++ 中一样。你可以在一个enum类型上有一个方法,而不是写一个switch,只需调用该方法 - 一行代码:完成!

enum MyEnum {
    SOME_ENUM_CONSTANT {
        @Override
        public void method() {
            System.out.println("first enum constant behavior!");
        }
    },
    ANOTHER_ENUM_CONSTANT {
        @Override
        public void method() {
            System.out.println("second enum constant behavior!");
        }
    }; // note the semi-colon after the final constant, not just a comma!
    public abstract void method(); // could also be in an interface that MyEnum implements
}

void aMethodSomewhere(final MyEnum e) {
    doSomeStuff();
    e.method(); // here is where the switch would be, now it's one line of code!
    doSomeOtherStuff();
}