编程语言
首页 > 编程语言> > Java 枚举类型

Java 枚举类型

作者:互联网

枚举类方法

public enum Season{
    SPRING, SUMMER, AUTUMN, WINTER
}

扩展枚举值与自定义构造函数

public enum Season{
    SPRING("春"),
    SUMMER("夏"),
    AUTUMN("秋"),
    WINTER("冬");
    
    private ChineseName;
    
    private Season(String ChineseName){
        this.ChineseName = ChineseName;
    }
}

枚举类中定义抽象方法

public enum Season{
    SPRING("春") {
        @Override
        void say(){
            System.out.println("Spring!!");
        }
    },
    SUMMER("夏"){
        @Override
        void say(){
            System.out.println("Summer!!");
        }
    },
    AUTUMN("秋"){
        @Override
        void say(){
            System.out.println("Autumn!!");
        }
    },
    WINTER("冬"){
        @Override
        void say(){
            System.out.println("Winter!!");
        }
    };
    
    private ChineseName;
    
    private Season(String ChineseName){
        this.ChineseName = ChineseName;
    }
    
    abstract void say();
}

标签:Java,Season,void,say,System,枚举,ChineseName,类型
来源: https://www.cnblogs.com/yuking28/p/15772297.html