5、枚举Enum

枚举类会隐式的继承Enum类,无法再继承其它类(单继承机制

一、无实参枚举类型:

1、定义:

/**
 * 1、无实参枚举类型
 */
public enum NoParamTypeEnums {
    SPRING,
    SUMMER,
    AUTUMN,
    WINTER;

    NoParamTypeEnums() {
    }
}

2、使用:

    public static void noParamTypeDemo() {
        String season = "SPRING";
        System.out.println("无实参枚举查询常量对象名结果:" + NoParamTypeEnums.SPRING.name());   //toPrint->无实参枚举查询常量对象名结果:SPRING
        if (season.equals(NoParamTypeEnums.SPRING.toString())) {
            System.out.println(season); //toPrint->SPRING
        }
    }

 

二、单实参枚举类型:

1、定义:

/**
 * 2、单实参枚举类型
 */
public enum OnlyParamTypeEnums {
    SPRING("春"),
    SUMMER("夏"),
    AUTUMN("秋"),
    WINTER("冬");

    String key;

    OnlyParamTypeEnums(String key) {
        this.key = key;
    }

    //获取枚举的key:春、夏、秋、冬
    public String getKey() {
        return key;
    }

    //输入枚举的key(春、夏、秋、冬),获取对应的常量对象名(SPRING、SUMMER、AUTUMN、WINTER)
    public static String getKeyObjectName(String key) {
        String keyName = null;
        for (int i = 0; i < OnlyParamTypeEnums.values().length; i++) {
            if (key.equals(OnlyParamTypeEnums.values()[i].getKey())) {
                keyName = OnlyParamTypeEnums.values()[i].name();
                break;
            }
        }
        return keyName;
    }
}

2、使用:

    public static void onlyParamTypeDemo() {
        String season = "春";
        System.out.println("单实参枚举查询key结果:" + OnlyParamTypeEnums.SPRING.getKey());   //toPrint->单实参枚举查询key结果:春
        System.out.println("单实参枚举查询常量对象名结果:" + OnlyParamTypeEnums.SPRING.name());   //toPrint->单实参枚举查询常量对象名结果:SPRING
        System.out.println("单实参枚举根据key查询常量对象名结果:" + OnlyParamTypeEnums.getKeyObjectName(season));   //toPrint->单实参枚举根据key查询常量对象名结果:SPRING
        if (season.equals(OnlyParamTypeEnums.SPRING.getKey())) {
            System.out.println(season); //toPrint->春
        }
    }

 

三、双实参枚举类型:

1、定义:

/**
 * 3、双实参枚举类型
 */
public enum DoubleParamTypeEnums {
    SPRING("0", ""),
    SUMMER("1", ""),
    AUTUMN("2", ""),
    WINTER("3", "");

    String key;
    String value;

    DoubleParamTypeEnums(String key, String value) {
        this.key = key;
        this.value = value;
    }

    //获取枚举的key:0、1、2、3
    public String getKey() {
        return key;
    }

    //输入枚举的value(春、夏、秋、冬),获取枚举的key:0、1、2、3
    public static String getKey(String value) {
        String keyName = null;
        for (int i = 0; i < DoubleParamTypeEnums.values().length; i++) {
            if (value.equals(DoubleParamTypeEnums.values()[i].getValue())) {
                keyName = DoubleParamTypeEnums.values()[i].getKey();
                break;
            }
        }
        return keyName;
    }

    //输入枚举的key(0、1、2、3),获取对应的常量对象名(SPRING、SUMMER、AUTUMN、WINTER)
    public static String getKeyObjectName(String key) {
        String keyName = null;
        for (int i = 0; i < DoubleParamTypeEnums.values().length; i++) {
            if (key.equals(DoubleParamTypeEnums.values()[i].getKey())) {
                keyName = DoubleParamTypeEnums.values()[i].name();
                break;
            }
        }
        return keyName;
    }

    //获取枚举的value:春、夏、秋、冬
    public String getValue() {
        return value;
    }

    //输入枚举的key(0、1、2、3),获取枚举的value:春、夏、秋、冬
    public static String getValue(String key) {
        String valueName = null;
        for (int i = 0; i < DoubleParamTypeEnums.values().length; i++) {
            if (key.equals(DoubleParamTypeEnums.values()[i].getKey())) {
                valueName = DoubleParamTypeEnums.values()[i].getValue();
                break;
            }
        }
        return valueName;
    }

    //输入枚举的key(春、夏、秋、冬),获取对应的常量对象名(SPRING、SUMMER、AUTUMN、WINTER)
    public static String getValueObjectName(String value) {
        String keyName = null;
        for (int i = 0; i < DoubleParamTypeEnums.values().length; i++) {
            if (value.equals(DoubleParamTypeEnums.values()[i].getValue())) {
                keyName = DoubleParamTypeEnums.values()[i].name();
                break;
            }
        }
        return keyName;
    }

    //获取双实参枚举Map集合
    public static Map<String, String> getDataMap() {
        Map<String, String> dataMap = new HashMap<>();
        for (int i = 0; i < DoubleParamTypeEnums.values().length; i++) {
            String key = DoubleParamTypeEnums.values()[i].getKey();
            String value = DoubleParamTypeEnums.values()[i].getValue();
            dataMap.put(key, value);
        }
        return dataMap;
    }

private static final List<String> DoubleParamTypeList = Arrays.asList( SPRING.getKey(), SUMMER.getKey(), AUTUMN.getKey(), WINTER.getKey()); public static List<String> getDoubleParamTypeList (){ return DoubleParamTypeList ; } }

2、使用:

    public static void doubleParamTypeDemo() {
        String seasonKey = "0";
        String seasonValue = "春";
        System.out.println("双实参枚举查询key结果:" + DoubleParamTypeEnums.SPRING.getKey());   //toPrint->双实参枚举查询key结果:0
        System.out.println("双实参枚举根据value查询key结果:" + DoubleParamTypeEnums.getKey(seasonValue));   //toPrint->双实参枚举根据value查询key结果:0
        System.out.println("双实参枚举根据key查询常量对象名结果:" + DoubleParamTypeEnums.getKeyObjectName(seasonKey));   //toPrint->双实参枚举根据key查询常量对象名结果:SPRING
        System.out.println("双实参枚举查询value结果:" + DoubleParamTypeEnums.SPRING.getValue());   //toPrint->双实参枚举查询value结果:春
        System.out.println("双实参枚举根据key查询value结果:" + DoubleParamTypeEnums.getValue(seasonKey));   //toPrint->双实参枚举根据key查询value结果:春
        System.out.println("双实参枚举根据value查询常量对象名结果:" + DoubleParamTypeEnums.getValueObjectName(seasonValue));   //toPrint->双实参枚举根据value查询常量对象名结果:SPRING
        System.out.println("双实参枚举Map集合结果:" + DoubleParamTypeEnums.getDataMap());   //toPrint->双实参枚举Map集合结果:{0=春, 1=夏, 2=秋, 3=冬}
        System.out.println("双实参枚举Map集合查询value结果:" + DoubleParamTypeEnums.getDataMap().get(seasonKey));   //toPrint->双实参枚举Map集合查询value结果:春
        if (seasonKey.equals(DoubleParamTypeEnums.SPRING.getKey())) {
            System.out.println(seasonKey);  //toPrint->0
        }
    }

 

posted on 2022-08-14 22:44  爱文(Iven)  阅读(56)  评论(0编辑  收藏  举报

导航