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类实现接口,即可以对所有的枚举成员实现同样的虚方法,也可以针对特殊的枚举成员实现虚方法.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); } }