OCP 关于switch的坑

来源:互联网 发布:bmp和jpg的区别 知乎 编辑:程序博客网 时间:2024/06/05 10:40

1. switch case 标签如果是enum type,那么必须使用enum 的值,不能有enum的类名.

2. switch case 标签如果没有任何语句,那么就不需要分号结尾。

3. switch case 的标签必须是唯一的而且是编译时常量。

4. switch case 标签的类必须和switch 表达式的类一样。


public class Vocation {    public static void main(String... unused){        final DaysOff input = DaysOff.Thanksgiving;        switch(input){            default:   // 可以允许是空白,不需要分号            case ValentinesDay:            case PresidentsDay.ValentinesDay: // 不能前缀enum 名,必须只使用enum值,                //显示的错误信息是 an enum swich case label must be the unqualified                // name of an enumeration constant                System.out.println(2);        }    }}enum DaysOff{    Thanksgiving, PresidentsDay, ValentinesDay}


原创粉丝点击