java enum 枚举配合switch

葫芦的运维日志

下一篇 搜索 上一篇

2015/01/30 01:36



package net.liezi.java.learn.initialization;

/**
 * @author 王梓
 */
public class EnumOrder {
       Spinciness degree;
        public EnumOrder(Spinciness d ){
               this.degree =d ;
       }
        public void describe(){
              System. out.print("This burrito is:" );

               switch (degree ) {
               case NOT : System.out.println( "not spicy at all.");
                     
                      break;
               case MILD :
               case MEDIUM :System.out.println("a little hot.");
                      break;
               case HOT :
               case FLAMING :
               default: System.out.println("maybe too hot");
              }
       }
public static void main(String[] args){
       
       Spinciness howHot=Spinciness. MEDIUM;
       System. out.println(howHot +"\n" );
       
        for(Spinciness s :Spinciness.values()){
              System. out.println(s +".ordinal " +s .ordinal());
       }
       
       
       
       EnumOrder
               plain= new EnumOrder(Spinciness.NOT),
               greenChile=new EnumOrder(Spinciness.MEDIUM),
               jalapeno= new EnumOrder(Spinciness.HOT);
        plain.describe();
        greenChile.describe();
        jalapeno.describe();

}
}/*output
MEDIUM

NOT.ordinal 0
MILD.ordinal 1
MEDIUM.ordinal 2
HOT.ordinal 3
FLAMING.ordinal 4
This burrito is:not spicy at all.
This burrito is:a little hot.
This burrito is:maybe too hot

*/

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 打赏联系方式 陕ICP备17005322号