public enum ColorPictures { //注:枚举写在最前面,否则编译出错 ColorWhite("白色", 1), ColorYellow("黄色", 2), ColorGold("金色", 3), ColorOrange("橙色", 4), ColorRosiness("玫瑰", 5), ColorPink("粉红", 6), ColorRed("红色", 7), ColorSilver("银色", 8), ColorBrown("棕色", 9), ColorBrownness("褐色", 10), ColorPurple("紫色", 11), ColorCyan("青色", 12), ColorGreen("绿色", 13), ColorBlue("蓝色", 14), ColorBlack("黑色", 15), ColorGray("灰色", 16), ColorBlackishGreen("墨绿", 17), ColorDarkBlue("深蓝", 18), ColorDarkRed("深红", 19); private final String typeName; private final int number; private ColorPictures(String typeName, int number) { this.typeName = typeName; this.number = number; } /** * 通过 Val 的数值获取枚举实例名称 * * @param val * @return */ public static String getTypeName(int val) { for (ColorPictures type : ColorPictures.values()) { if (type.getNumber() == val) { return type.getTypeName(); } } return null; //白色 } /** * 通过 Val 的数值获取枚举实例 * * @param val * @return */ public static ColorPictures getType(int val) { for (ColorPictures type : ColorPictures.values()) { if (type.getNumber() == val) { return type; } } return null; //ColorWhite } public String getTypeName() { return typeName; } public int getNumber() { return number; } public static void main(String[] args) { System.out.println(getTypeName(1)); } }
枚举的应用-1
最新推荐文章于 2021-07-02 20:30:40 发布