Jafe Lee

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  49 随笔 :: 0 文章 :: 24 评论 :: 0 Trackbacks
当枚举类型用在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
posted on 2007-09-08 14:43 Jafe Lee 阅读(7479) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: