★16.枚举类型

Enum

简介

  • enum不能被继承。
  • enum的构造函数自动为私有。
  • 除了以上两点,enum与类相同。
  • 可以通过以下两种方法获取所有枚举值:
    • 调用values静态方法,返回值类型为数组。
    • 调用Class对象中的getEnumConstants方法。

简单示例

public enum OzWitch {
    WEST("Miss Gulch, aka the Wicked Witch of the West"),
    NORTH("Glinda, the Good Witch of the North"),
    EAST("Wicked Witch of the East, wearer of the Ruby " + "Slippers, crushed by Dorothy's house"),
    SOUTH("Good by inference, but missing");

    private String description;

    OzWitch(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public static void main(String[] args) {
        for (OzWitch witch : OzWitch.values())
            System.out.println(witch + ": " + witch.getDescription());
    }
}

EnumSet

public class EnumSetDemo {
    private enum Color {
        RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255);

        private int r;
        private int g;
        private int b;

        private Color(int r, int g, int b) {
            this.r = r;
            this.g = g;
            this.b = b;
        }
    }

    public static void main(String args[]) {
        EnumSet yellow = EnumSet.of(Color.RED, Color.GREEN);
        drawLine(yellow);
        EnumSet white = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE);
        drawLine(white);
        EnumSet pink = EnumSet.of(Color.RED, Color.BLUE);
        drawLine(pink);
    }

    private static void drawLine(Set colors) {
        System.out.println("Requested Colors to draw lines : " + colors);
        for (Color c : colors) {
            System.out.println("drawing line in color : " + c);
        }
    }
}

EnumMap

public class EnumMapDemo {
    private enum STATE {NEW, RUNNING, WAITING, FINISHED}

    public static void main(String args[]) {
        // 使用STATE枚举类型作为Map的键创建EnumMap
        EnumMap stateMap = new EnumMap<>(STATE.class);
        System.out.println("EnumMap: " + stateMap);

        stateMap.put(STATE.RUNNING, "Program is running");
        stateMap.put(STATE.WAITING, "Program is waiting");
        stateMap.put(STATE.NEW, "Program has just created");
        stateMap.put(STATE.FINISHED, "Program has finished");

        System.out.println("Size of EnumMap in java: " + stateMap.size());
        System.out.println("EnumMap: " + stateMap);
        System.out.println("EnumMap key : " + STATE.NEW + " value: " + stateMap.get(STATE.NEW));

        for (STATE currentState : stateMap.keySet()) {
            System.out.println("key : " + currentState + " value : " + stateMap.get(currentState));
        }

        System.out.println("Does stateMap has :" + STATE.NEW + " : " + stateMap.containsKey(STATE.NEW));
        System.out.println("Does stateMap has :" + "Program has finished" + " : " + stateMap.containsValue("Program has finished"));
    }
}

多路分发

public enum ConstantSpecificMethod {
    DATE_TIME {
        String getInfo() { return DateFormat.getDateInstance().format(new Date()); }
    },
    CLASSPATH {
        String getInfo() { return System.getenv("CLASSPATH"); }
    },
    VERSION {
        String getInfo() { return System.getProperty("java.version"); }
    };

    abstract String getInfo();

    public static void main(String[] args) {
        for (ConstantSpecificMethod csm : values())
            System.out.println(csm.getInfo());
    }
}

你可能感兴趣的:(★16.枚举类型)