2020-04-15 |

枚举类型

声明枚举后,我们可以使用switch语句检查相应的值。

Rank a = Rank.SOLDIER;

switch(a) {
    case SOLDIER:
    System.out.println("Soldier says hi!");
    break;
    case SERGEANT:
    System.out.println("Sergeant says Hello!");
    break;
    case CAPTAIN:
    System.out.println("Captain says Welcome!");
    break;
}
//输出 "Soldier says hi!"

0

发表评论

    评价:
    验证码: 点击我更换图片
    最新评论