Java 列舉(enum)
阿新 • • 發佈:2018-12-28
學習原博 https://blog.csdn.net/qq_27093465/article/details/52180865
package com.example.demo.bean.baseBean; import org.springframework.util.StringUtils; import java.util.Arrays; /** * @Author: * @Date: 2018/12/21 09:06 * @Description: */ public class constructionBean { /** * 應用型別列舉 */ enum AppTypeEnum { ANDROID, IOS, WINDOWS } /** * 顏色列舉 */ enum ColorEnmu { RED, GREEN, YELLOW } /** * 訊號燈轉換 * * @param color * @return */ public static ColorEnmu changTraficColor(ColorEnmu color) { switch (color) { case RED: color = ColorEnmu.GREEN; break; case GREEN: color = ColorEnmu.YELLOW; break; case YELLOW: color = ColorEnmu.RED; break; } return color; } /** * 班級列舉值 */ public enum ClassEnum { CLASS_ENUM1(1, "一班"), CLASS_ENUM2(2, "二班"), CLASS_ENUM3(3, "三班"); private int index; private String name; ClassEnum(int index, String name) { this.index = index; this.name = name; } public static String getNameByIndex(int a) { String name = null; for (ClassEnum classEnum : ClassEnum.values()) { if (a == classEnum.getIndex()) { return classEnum.getName(); } } return name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void main(String[] args) { // for (AppTypeEnum a :AppTypeEnum.values()) { // System.out.println(a); // } // System.out.println(AppTypeEnum.valueOf("IOS")); // ColorEnmu colorEnmu = ColorEnmu.RED; // for (int i=1;i<10;i++){ // ColorEnmu colorEnmuTemp = colorEnmu; // colorEnmu = changTraficColor(colorEnmu); // System.out.println("第"+i+"次燈色由【"+colorEnmuTemp+"】變成【"+colorEnmu+"】"); // } int c = 4; System.out.println(ClassEnum.getNameByIndex(c)); } }