enum类型用于switch语句

 1 import java.util.*;
 2 public class Demo13{
 3     
 4     
 5     public static void main(String[] args) {
 6         
 7         Weekday day = Weekday.SUN;
 8         
 9         //将enum类型用于switch语句
10         //因为枚举类天生具有类型信息和有限个枚举常量,所以比int、String类型更适合用在switch语句中:
11         switch(day){
12             //注意:case 后面是空格 加上常量 最后冒号
13             case MON:
14             case TUE:
15             case WED:
16             case THU:
17             case FRI:
18                 System.out.println("Today is " + day +". Work at office!");
19                 break;
20             case SAT:
21             case SUN:
22                 System.out.println("Today is " + day +". Work at home!");
23                 break;
24             default:
25                 System.out.println("cannot process " + day);
26         }
27     }
28 }
29 
30 enum Weekday{
31     MON,TUE,WED,THU,FRI,SAT,SUN;
32 }

 

posted @ 2020-12-27 11:44  dog_IT  阅读(533)  评论(0编辑  收藏  举报