声明枚举后,我们可以使用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!"