Java實例講解枚舉enum的實現

一.枚舉介紹

1.枚舉是一組常量的集合

2.理解:美劇屬於一種特殊的類,裡面隻包含一組有限的特定的對象

1.自定義枚舉實現

1.不需要提供Set方法,枚舉對象值通常為隻讀。

2.對枚舉對象/屬性使用final和static關鍵字修飾,實現底層優化

3.枚舉對象名通常使用全部大寫,常量的命名規范

4.枚舉·對象根據需要,可以有一到多個屬性

(1)示例

package com.demo;
import javax.sound.midi.Soundbank;
/**
 * @version 1.0
 * @auther Demo龍
 */
public class Enum01 {
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.AUTUMN);
        System.out.println(Season.WINTER);
    }
}
//演示自定義枚舉實現
class Season{
    private String name;
    private String desc;//描述特性
    //1.將構造器私有化,目的防止直接new出來
    private Season(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }
    //2.去掉set方法,隻能讀取,不能修改屬性
    public String getName() {
        return name;
    }
    public String getDesc() {
        return desc;
    }
    //3.在season內部創建固定的對象
    //定義瞭四個對象
//    public static Season SPRING=new Season("春天","溫暖");
//    public static Season SUMMER=new Season("夏天","炎熱");
//    public static Season AUTUMN=new Season("秋天","涼爽");
//    public static Season WINTER=new Season("冬天","寒冷");
    //最終優化,加入final
    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("冬天","寒冷");
    //重寫toString方法
    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

(2)測試結果

2.enum關鍵字實現枚舉

如果使用enum關鍵字來實現枚舉類

1.使用關鍵字enum替換class

2.public static final Season02 SPRING=new Season02(“春天”,“溫暖”);可替換為 SPRING(“春天”,“溫暖”),//解讀:常量名(實參列表)

3.如果有多個常量使用逗號間隔即可

4.如果使用enum關鍵字實現枚舉,要求將定義的常量對象寫下前面

(1)示例

package com.demo;
/**
 * @version 1.0
 * @auther Demo龍
 */
public class Enum02 {
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.AUTUMN);
        System.out.println(Season.WINTER);
    }
}
//演示使用enum關鍵字來實現枚舉類
enum Season02{
    //如果使用enum關鍵字來實現枚舉類
    //1.使用關鍵字enum替換class
    //2.public static final Season02 SPRING=new Season02("春天","溫暖");可替換為
    SPRING("春天","溫暖"),//解讀:常量名(實參列表)
    //3.如果有多個常量使用逗號間隔即可
    SUMMER("夏天","炎熱"),
    AUTUMN("秋天","涼爽"), WINTER("冬天","寒冷");
    //4.如果使用enum關鍵字實現枚舉,要求將定義的常量對象寫下前面
    private String name;
    private String desc;//描述特性
    //1.將構造器私有化,目的防止直接new出來
    private Season02(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }
    //2.去掉set方法,隻能讀取,不能修改屬性
    public String getName() {
        return name;
    }
    public String getDesc() {
        return desc;
    }
    //3.在season內部創建固定的對象
    //定義瞭四個對象
//    public static Season SPRING=new Season("春天","溫暖");
//    public static Season SUMMER=new Season("夏天","炎熱");
//    public static Season AUTUMN=new Season("秋天","涼爽");
//    public static Season WINTER=new Season("冬天","寒冷");
    //最終優化,加入final
//    public static final Season02 SPRING=new Season02("春天","溫暖");
//    public static final Season02 SUMMER=new Season02("夏天","炎熱");
//    public static final Season02 AUTUMN=new Season02("秋天","涼爽");
//    public static final Season02 WINTER=new Season02("冬天","寒冷");
    //重寫toString方法
    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

(2)測試結果

3.enum關鍵字註意事項

1.當我i們使用enum關鍵字開發一個枚舉類時,默認會繼承Enum類

2.傳統的public static final Season02 SPRING=new Season02("春天","溫暖");,這裡要知道他調用的是哪個構造器

3.如果使用無參構造器創建枚舉對象,則實參列表和小括號都可以省略.

4.當有多個枚舉對象時,使用逗號間隔,最後有一個分號結尾

5.枚舉對象必須放在枚舉類的行首

(1)示例

package com.demo;
/**
 * @version 1.0
 * @auther Demo龍
 */
public class Enum02 {
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.AUTUMN);
        System.out.println(Season.WINTER);
    }
}
//演示使用enum關鍵字來實現枚舉類
enum Season02{
    //如果使用enum關鍵字來實現枚舉類
    //1.使用關鍵字enum替換class
    //2.public static final Season02 SPRING=new Season02("春天","溫暖");可替換為
    SPRING("春天","溫暖"),//解讀:常量名(實參列表)
    //3.如果有多個常量使用逗號間隔即可
    SUMMER("夏天","炎熱"),
    AUTUMN("秋天","涼爽"), WINTER("冬天","寒冷"),
    //3.如果使用無參構造器創建枚舉對象,則實參列表和小括號都可以省略.兩種寫法
    HAHA,//調用無參構造器
    HAHAHA();//調用無參構造器
    //4.如果使用enum關鍵字實現枚舉,要求將定義的常量對象寫下前面
    private String name;
    private String desc;//描述特性
    //1.將構造器私有化,目的防止直接new出來
    private Season02(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }
    private Season02() {
        //無參構造器
    }
    //2.去掉set方法,隻能讀取,不能修改屬性
    public String getName() {
        return name;
    }
    public String getDesc() {
        return desc;
    }
    //3.在season內部創建固定的對象
    //定義瞭四個對象
//    public static Season SPRING=new Season("春天","溫暖");
//    public static Season SUMMER=new Season("夏天","炎熱");
//    public static Season AUTUMN=new Season("秋天","涼爽");
//    public static Season WINTER=new Season("冬天","寒冷");
    //最終優化,加入final
//    public static final Season02 SPRING=new Season02("春天","溫暖");
//    public static final Season02 SUMMER=new Season02("夏天","炎熱");
//    public static final Season02 AUTUMN=new Season02("秋天","涼爽");
//    public static final Season02 WINTER=new Season02("冬天","寒冷");
    //重寫toString方法
    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

到此這篇關於Java實例講解枚舉enum的實現的文章就介紹到這瞭,更多相關Java枚舉內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: