枚舉

1 枚舉類:類的對象是有限個的。spring

2 自定義枚舉類: ide

package lianxi3;

public class TestSeason {
    public static void main(String[] args){
        System.out.println(Season.SPRING.getSeasonName());
        Season spring = Season.SPRING;
        spring.show();
        System.out.println(spring);
    }
    
    
}
class Season{
    private final String seasonName;
    private final String seasonDesc;
    public Season(String seasonName, String seasonDesc) {
        super();
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    public String getSeasonName() {
        return seasonName;
    }
    public String getSeasonDesc() {
        return seasonDesc;
    }
    //建立枚舉類的對象
    public static final Season SPRING = new Season("春天","春暖花開");
    public static final Season SUMMER = new Season("夏天","夏日炎炎");
    public static final Season AUTUMN = new Season("秋天","秋高氣爽");
    public static final Season WINTER = new Season("冬天","白雪皚皚");
    @Override
    public String toString() {
        return "Season [seasonDesc=" + seasonDesc + ", seasonName="
                + seasonName + "]";
    }
    public void show(){
        System.out.println("一年四季");
    }
    
}

結果:this

春天
一年四季
Season [seasonDesc=春暖花開, seasonName=春天]
3 enum關鍵字定義枚舉類:經常使用方法:values() valueOf(String name)spa

如何讓枚舉類實現接口:讓不一樣的枚舉類對象調用被重寫的抽象方法,執行效果不一樣。code

package lianxi3;

public class TestSeason2 {
    public static void main(String[] args){
//        System.out.println(Season2.SPRING.getSeasonName());
//        Season2 spring = Season2.SPRING;
//        spring.show();
//        System.out.println(spring);
//        System.out.println();
        Season2[] seasons = Season2.values();
        for(int i=0;i<seasons.length;i++){
            System.out.println(seasons[i]);
            seasons[i].show();
        }
        System.out.println();
        String str = "AUTUMN";
        Season2 sea = Season2.valueOf(str);
        System.out.println(sea);
        sea.show();
    }
    
    
}
interface season{
    public void show();
}
enum Season2 implements season{
    //建立枚舉類的對象
    SPRING("春天","春暖花開"){
        public void show(){
            System.out.println("美麗的春天");
        }
    },
    SUMMER("夏天","夏日炎炎"){
        public void show(){
            System.out.println("美麗的夏天");
        }
    },
    AUTUMN("秋天","秋高氣爽"){
        public void show(){
            System.out.println("美麗的秋天");
        }
    },
    WINTER("冬天","白雪皚皚"){
        public void show(){
            System.out.println("美麗的冬天");
        }
    };
    private final String seasonName;
    private final String seasonDesc;
    private Season2(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    public String getSeasonName() {
        return seasonName;
    }
    public String getSeasonDesc() {
        return seasonDesc;
    }
    
    @Override
    public String toString() {
        return "Season2 [seasonDesc=" + seasonDesc + ", seasonName="
                + seasonName + "]";
    }
    public void show(){
        System.out.println("一年四季");
    }
    
}

結果:對象

Season2 [seasonDesc=春暖花開, seasonName=春天]
美麗的春天
Season2 [seasonDesc=夏日炎炎, seasonName=夏天]
美麗的夏天
Season2 [seasonDesc=秋高氣爽, seasonName=秋天]
美麗的秋天
Season2 [seasonDesc=白雪皚皚, seasonName=冬天]
美麗的冬天blog

Season2 [seasonDesc=秋高氣爽, seasonName=秋天]
美麗的秋天 接口

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息