Java核心技术-枚举

 1 import java.util.*;
 2 
 3 public class EnumTest {
 4     public static void main(String[] args) {
 5         Scanner in = new Scanner(System.in);
 6         System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
 7         String input = in.next().toUpperCase();
 8         Size size = Enum.valueOf(Size.class, input);
 9         System.out.println("size=" + size);
10         System.out.println("abbreviation=" + size.getAbbreviation());
11         if (size == Size.EXTRA_LARGE)
12             System.out.println("Good job--you paid attention to the _.");
13     }
14 }
15 
16 enum Size {
17     SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
18 
19     private Size(String abbreviation) {
20         this.abbreviation = abbreviation;
21     }
22 
23     public String getAbbreviation() {
24         return abbreviation;
25     }
26 
27     private String abbreviation;
28 }

猜你喜欢

转载自www.cnblogs.com/llawliet0001/p/10024948.html
今日推荐