- 论坛徽章:
- 0
|
java enum - 1 package com.karl.test;
- 2
- 3 public class TestEnum {
- 4
- 5 public enum ColorSelect{
- 6 red,green,yellow,blue;
- 7 }
- 8
- 9 public enum Season{
- 10 spring,summer,fall, winter;
- 11 private final static String location = "Phoenix";
- 12
- 13 public static Season getBest(){
- 14 if(location.endsWith("Phoenix"))
- 15 return winter;
- 16 else
- 17 return summer;
- 18 }
- 19 }
- 20
- 21 public enum Temp{
- 22 absoluteZero(-459), freezing(32), boiling(212), paperBurns(451);
- 23 private final int value;
- 24 public int getValue(){
- 25 return value;
- 26 }
- 27 Temp(int value){
- 28 this.value = value;
- 29 }
- 30 }
- 31
- 32
- 33 public static void main(String[] args) {
- 34 ColorSelect m = ColorSelect.blue;
- 35 switch(m){
- 36 case red:
- 37 System.out.println("color is red");
- 38 break;
- 39 case green:
- 40 System.out.println("color is green");
- 41 break;
- 42 case yellow:
- 43 System.out.println("color is yellow");
- 44 break;
- 45 case blue:
- 46 System.out.println("color is blue");
- 47 break;
- 48 }
- 49
- 50 for(ColorSelect c : ColorSelect.values()){
- 51 System.out.println(c);
- 52 }
- 53
- 54 System.out.println("total " + ColorSelect.values().length + " in ColorSelect.");
- 55
- 56 System.out.println("the position of blue in ColorSelect is " + ColorSelect.blue.ordinal());
- 57
- 58 System.out.println("compare red to green " + ColorSelect.red.compareTo(ColorSelect.green));
- 59
- 60 System.out.println("Season.getBest()= " + Season.getBest());
- 61
- 62 for (Temp t : Temp.values()) {
- 63 System.out.println("The value of t is " + t.getValue());
- 64 }
- 65
- 66 }
- 67 }
复制代码 |
|