这个功能会被放到后面的Java版本中吗?

有人能解释一下为什么我不能这样做,比如,Java的switch语句工作的技术方式吗?


当前回答

如果代码中有一个地方可以打开String,那么最好将String重构为可能值的枚举,这样就可以打开String。当然,可以将string的潜在值限制为枚举中的值,这可能是需要的,也可能不是需要的。

当然,你的枚举可以有一个'other'条目,和一个fromString(String)方法,然后你可以有

ValueEnum enumval = ValueEnum.fromString(myString);
switch (enumval) {
   case MILK: lap(); break;
   case WATER: sip(); break;
   case BEER: quaff(); break;
   case OTHER: 
   default: dance(); break;
}

其他回答

除了以上好的论点,我还要补充一点,今天很多人认为switch是Java过去过程化的过时残余(回到C时代)。

我不完全同意这个观点,我认为switch在某些情况下有它的用处,至少因为它的速度,无论如何,它比我在一些代码中看到的一系列级联数字要好…

但实际上,有必要考虑一下需要开关的情况,看看它是否不能被更面向对象的东西所取代。例如Java 1.5+中的枚举,也许是哈希表或其他集合(有时我后悔我们没有(匿名)函数作为第一类公民,就像Lua -没有开关-或JavaScript),甚至是多态性。

从1.7开始直接使用String的例子如下:

public static void main(String[] args) {

    switch (args[0]) {
        case "Monday":
        case "Tuesday":
        case "Wednesday":
            System.out.println("boring");
            break;
        case "Thursday":
            System.out.println("getting better");
        case "Friday":
        case "Saturday":
        case "Sunday":
            System.out.println("much better");
            break;
    }

}

不是很漂亮,但这里是Java 6的另一种方式:

String runFct = 
        queryType.equals("eq") ? "method1":
        queryType.equals("L_L")? "method2":
        queryType.equals("L_R")? "method3":
        queryType.equals("L_LR")? "method4":
            "method5";
Method m = this.getClass().getMethod(runFct);
m.invoke(this);

James Curran简洁地说:“基于整数的开关可以优化为非常高效的代码。基于其他数据类型的开关只能编译为一系列if()语句。因此,C和c++只允许在整数类型上切换,因为这对其他类型是没有意义的。”

My opinion, and it's only that, is that as soon as you start switching on non-primitives you need to start thinking about "equals" versus "==". Firstly comparing two strings can be a fairly lengthy procedure, adding to the performance problems that are mentioned above. Secondly if there is switching on strings there will be demand for switching on strings ignoring case, switching on strings considering/ignoring locale,switching on strings based on regex.... I would approve of a decision that saved a lot of time for the language developers at the cost of a small amount of time for programmers.

如果代码中有一个地方可以打开String,那么最好将String重构为可能值的枚举,这样就可以打开String。当然,可以将string的潜在值限制为枚举中的值,这可能是需要的,也可能不是需要的。

当然,你的枚举可以有一个'other'条目,和一个fromString(String)方法,然后你可以有

ValueEnum enumval = ValueEnum.fromString(myString);
switch (enumval) {
   case MILK: lap(); break;
   case WATER: sip(); break;
   case BEER: quaff(); break;
   case OTHER: 
   default: dance(); break;
}