Java中枚举方法举例以及switch分析

Posted on 2013-03-15 16:59  JasmineLiu  阅读(7716)  评论(0编辑  收藏  举报

public class EnumDemo
{

private enum Season
{
spring,summer,fall,winter
}

private static void doIt(Season s)
{
switch (s)
{
case spring:
System.out.println("value is " + Season.spring);
case summer:
System.out.println("value is " + Season.summer);
break;
case fall:
System.out.println("value is " + Season.fall);
case winter:
System.out.println("value is " + Season.winter);
break;
default:
System.out.println("default");
}
}

public static void main(String[] args)
{
for(Season s: Season.values())
{
      System.out.println(s);
}
doIt(Season.fall);
}

}

 

以上例子中:

for(Season s: Season.values())
{
      System.out.println(s); 
}的输出结果是:spring

                    summer

                     fall

                     winter

doIt(Season.fall);的输出结果是:fall 

                                             winter

 switch(x) :从指定的case-x处开始执行 遇到break结束。                       

 

 

Copyright © 2024 JasmineLiu
Powered by .NET 9.0 on Kubernetes