枚举Enum
所谓的枚举就是规定好了指定的范围,所有的内容都是从指定的范围内取出。
举例:
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 }
举例:枚举enum
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