码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 枚举类

时间:2020-02-15 17:06:36      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:实现   枚举   sum   spring   desc   java教程   play   over   vat   

摘抄在 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 枚举类

标签:实现   枚举   sum   spring   desc   java教程   play   over   vat   

原文地址:https://www.cnblogs.com/superxuezhazha/p/12312622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!