java列舉類

沒有陽光,也一樣燦爛發表於2020-11-18

Java列舉類

定義列舉類

  • 列舉類的使用:

    • 定義:類的物件只有有限個,確定的,稱此類為列舉類
    • 當需要定義一組常量時,使用列舉類會很方便
    • 如果列舉類中只有一個物件,則可以作為單例模式的實現方式
  • 定義列舉類

    • 方式一:jdk5.0之前,自定義列舉類
    package www.bh.c.enumtest;
    
    public class Test1 {
        public static void main(String[] args) {
            Season spring = Season.SPRING;
            System.out.println(spring.seasonName);//春天
            System.out.println(spring.getSeasonName());//春天
        }
    
        static class Season {
            //1.宣告物件的屬性:private final修飾
            private final String seasonName;
            private final String seasonDesc;
            //2.建立私有的構造器,並給物件屬性賦值
            private Season(String seasonName,String seasonDesc) {
                this.seasonName=seasonName;
                this.seasonDesc=seasonDesc;
            }
            //提供當前列舉類的多個物件:public static 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("冬天","火鍋燒烤");
            //其他,獲取列舉類物件的屬性
            public String getSeasonName() {
                return seasonName;
            }
            public String getSeasonDesc() {
                return seasonDesc;
            }
        }
    }
    
    • 方式二:jdk5.0,使用enum關鍵字定義列舉類

使用關鍵字enum定義列舉類

package www.bh.c.enumtest;

public class Test2 {
    public static void main(String[] args) {
        Season summer=Season.SUMMER;
        System.out.println(summer.seasonName);//夏天
        System.out.println(summer.seasonDesc);//夏日炎炎
    }

    enum Season{
        //1.提供當前列舉類的物件,物件之間用","隔開,末尾物件";"結束
        SPRING("春天","春暖花開"),
        SUMMER("夏天","夏日炎炎"),
        AUTUMN("秋天","秋風陣陣"),
        WINTER("冬天","冬暖夏涼");
        //2.宣告物件的屬性:private final修飾
        private final String seasonName;
        private final String seasonDesc;
        //3.建立私有的構造器,並給物件屬性賦值
        Season(String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
        //其他,獲取列舉類物件的屬性
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }
    }
}

Enum類的主要方法

  • value()方法:返回列舉型別的物件陣列,方便遍歷所有的列舉值
package www.bh.c.enumtest;
import com.sun.jdi.Value;

public class Test2 {
    public static void main(String[] args) {
        Season[] seasons=Season.values();
        for (int i=0;i<seasons.length;i++){
        System.out.println(seasons[i]);//SPRING,SUMMER,AUTUMN,WINTER
        }
    }
  • valueOf(String str):可以把一個字串轉為對應的列舉類物件。要求字串必須是列舉類物件的’“名字”
package www.bh.c.enumtest;
import com.sun.jdi.Value;

public class Test2 {
    public static void main(String[] args) {
        Season spring=Season.valueOf("SPRING");
        System.out.println(spring);//SPRING
    }
}
  • toString():返回當前列舉類物件常量的名稱
package www.bh.c.enumtest;

public class Test2 {
    public static void main(String[] args) {
        Season summer=Season.SUMMER;
        System.out.println(summer.toString());//SUMMER
    }

相關文章