当枚举类型用在switch语句中时,语法有一点点特别。看例子:
package other;
import basic.Day;
public class SwitchTest
{
public static void main(String [] args)
{
int FRIDAY = 5;
System.out.println(FRIDAY);
Day today = Day.SATURDAY;
switch(today)
{
case FRIDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case SATURDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case SUNDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case MONDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case TUESDAT:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case WEDNESDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
case THURSDAY:
System.out.println("Today is " + today.toString().toLowerCase());
break;
default:
System.out.println("otherday");
}
}
}
注意在case标签中,Day不出现,即case Day.FRIDAY是不合法的。而在其他地方出现时则必须用Day.FRIDAY