JAVA 枚举类

Posted 行尸走肉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 枚举类相关的知识,希望对你有一定的参考价值。

摘抄在 b站尚硅谷JAVA教程

 

 

package com.LearnJava.enume;

enum Season{
    SPRING("春天","春暖花开"),
    SUMMER("夏天","夏日炎炎"),
    AUTUMO("秋天","秋高气爽"),
    WINTTER("冬天","白雪皑皑");

    private final String seasonName;
    private final String seasonDesc;


    Season(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
}
public class TestEnume {
    public static void main(String[] args) {
        Season spring = Season.SPRING;

        System.out.println(spring);
        System.out.println(spring.getClass().getSuperclass());
    }
}
//SPRING
//class com.LearnJava.enume.Season

 

 常用方法:

 

 

 

 enum类实现接口,即可以对所有的枚举成员实现同样的虚方法,也可以针对特殊的枚举成员实现虚方法.

package com.LearnJava.enume;

interface Show{
    void show();
}

enum Season implements Show{
    SPRING("春天","春暖花开"){
        @Override
        public void show() {
            System.out.println("我是春天");
        }
    },
    SUMMER("夏天","夏日炎炎"),
    AUTUMO("秋天","秋高气爽"),
    WINTTER("冬天","白雪皑皑");

    private final String seasonName;
    private final String seasonDesc;


    Season(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    @Override
    public void show() {
        System.out.println("这是一个季节");
    }
}
public class TestEnume {
    public static void main(String[] args) {
        Season spring = Season.SPRING;
        spring.show();
        Season summer = Season.SUMMER;
        summer.show();
    }
}
View Code

 

以上是关于JAVA 枚举类的主要内容,如果未能解决你的问题,请参考以下文章

JAVA枚举小结

比起睡觉,我更喜欢刷巨详细的Java枚举类,这是来自猿人的自觉呀

elasticsearch代码片段,及工具类SearchEsUtil.java

java枚举类的几点疑惑

关于java枚举类的一个问题

Java开发工程师高质量代码必备:枚举类