枚舉Enumthis
所謂的枚舉就是規定好了指定的範圍,全部的內容都是從指定的範圍內取出。spa
舉例:code
1 package com.feimao.a1; 2 3 4 class Color { 5 public static final Color RED = new Color("紅色"); 6 public static final Color GREEN = new Color("綠色"); 7 public static final Color YELLOW = new Color("黃色"); 8 private String name; 9 10 private Color(String name) { 11 this.name = name; 12 } 13 14 public void setName(String name) { 15 this.name = name; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public static Color getInstance(int i) { 23 switch (i) { 24 case 1: { 25 return RED; 26 } 27 case 2: { 28 return GREEN; 29 } 30 case 3: { 31 return YELLOW; 32 } 33 default: { 34 return null; 35 } 36 } 37 } 38 } 39 40 public class EnumDemo02 { 41 public static void main(String args[]) { 42 Color c1 = Color.RED; 43 System.out.println(c1.getName()); 44 Color c2 = Color.getInstance(2); 45 System.out.println(c2.getName()); 46 47 } 48 49 }
舉例:枚舉enumblog
1 enum Color{ 2 3 RED , GREEN ,YELLOW; 4 5 } 6 7 public class EnmuDemo01{ 8 9 public static void main(String args[]){ 10 11 Color c = Color.GREEN; 12 13 System.out.println(c); 14 15 } 16 17 } 18