background image

17             }
18         },
19         ExtraExtraLarge {
20             @Override
21             public double getPricingFactor() {
22                 return 1.2;
23             }
24         };
25         public double getPricingFactor() {
26             return 1.0;
27         }
28     }
29     public static void main(String args[]) {
30         for (Size s : Size.values()) {
31             double d = s.getPricingFactor();
32             System.out.println(s + " Size has pricing factor of " + d);
33         }
34     }
35     /*    结果如下:
36         Small Size has pricing factor of 0.8
37         Medium Size has pricing factor of 1.0
38         Large Size has pricing factor of 1.0
39         ExtraLarge Size has pricing factor of 1.2
40         ExtraExtraLarge Size has pricing factor of 1.2    */
  8. 枚举在 switch 语句中的用法,见如下代码:
1     public enum Color { 
2         RED, BLUE, BLACK, YELLOW 
3     } 
4     public static void main(String[] args) { 
5         Color m = Color.BLUE; 
6         //case

 

语句中引用枚举常量时不需要再加上枚举的类型名了。

7         switch (m) { 

       

 

  case RED: 

9             System.out.println("color is red");
10             break;
11         case BLACK:
12             System.out.println("color is black");
13             break;
14         case YELLOW:
15             System.out.println("color is yellow");
16             break;
17         case BLUE:
18             System.out.println("color is blue");
19             break;