枚举类
Posted yhqtv-com
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了枚举类相关的知识,希望对你有一定的参考价值。
1 package com.yhqtv.java; 2 3 /* 4 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 5 * @create 2020-05-02-9:46 6 *方式一:jdk5.0之前,自定义枚举类 7 * 方式二:jdk5.0 可以使用enum关键字定义枚举类 8 * 9 * 10 */ 11 public class SeasonTest { 12 public static void main(String[] args) { 13 Season summer = Season.SUMMER; 14 System.out.println(summer); 15 Season autumn = Season.AUTUMN; 16 System.out.println(autumn); 17 } 18 19 //自定义枚举类 20 } 21 class Season{ 22 //1.声明Season对象的属性:private final修饰 23 private final String seasonName; 24 private final String seasonDesc; 25 //2.私有化类的构造器,并给对象属性赋值 26 private Season(String seasonName,String seasonDesc){ 27 this.seasonDesc=seasonDesc; 28 this.seasonName=seasonName; 29 30 } 31 //3. 提供当前枚举类的多个对象:public static final 32 public static final Season SPRING=new Season("春天","春暖花开"); 33 public static final Season SUMMER=new Season("夏天","夏季炎炎"); 34 public static final Season AUTUMN=new Season("秋天","秋高气爽"); 35 public static final Season WINTER=new Season("冬天","冬雪皑皑"); 36 // 4.其他诉求1:获取枚举类对象的属性 37 public String getSeasonName() { 38 return seasonName; 39 } 40 41 public String getSeasonDesc() { 42 return seasonDesc; 43 } 44 45 @Override 46 public String toString() { 47 return "Season{" + 48 "seasonName=‘" + seasonName + ‘‘‘ + 49 ", seasonDesc=‘" + seasonDesc + ‘‘‘ + 50 ‘}‘; 51 } 52 53 }
1 package com.yhqtv.java; 2 3 /* 4 *使用enum关键字定义枚举类 5 * 说明:定义的枚举类默认继承于java.lang.Enum类 6 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 7 * @create 2020-05-02-10:23 8 * 9 */ 10 public class SeasonTest1 { 11 public static void main(String[] args) { 12 Season1 autumn = Season1.AUTUMN; 13 //toString(); 14 System.out.println(autumn.toString()); 15 16 // System.out.println(Season1.class.getSuperclass()); 17 System.out.println("--------------------"); 18 //values(); 19 Season1[] values = Season1.values(); 20 for (int i = 0; i < values.length; i++) { 21 System.out.println(values[i]); 22 values[i].show(); 23 } 24 System.out.println("--------------------"); 25 Thread.State[] values1 = Thread.State.values(); 26 for (int i = 0; i < values1.length; i++) { 27 System.out.println(values1[i]); 28 } 29 System.out.println("--------------------"); 30 //valueOf(String objName):返回枚举类中对象名是objName的对象 31 Season1 winter = Season1.valueOf("WINTER"); 32 System.out.println(winter); 33 winter.show(); 34 35 } 36 37 } 38 39 interface Info { 40 void show(); 41 42 } 43 44 enum Season1 implements Info { 45 //1.提供当前枚举类的对象,多个对象之间用“,”隔开,末尾用“;”结束 46 SPRING("春天", "春暖花开") { 47 @Override 48 public void show() { 49 System.out.println("春天在哪里"); 50 } 51 }, 52 SUMMER("夏天", "夏季炎炎") { 53 @Override 54 public void show() { 55 System.out.println("夏天好无聊"); 56 } 57 }, 58 AUTUMN("秋天", "秋高气爽") { 59 @Override 60 public void show() { 61 System.out.println("秋天很凉爽"); 62 } 63 }, 64 WINTER("冬天", "冬雪皑皑") { 65 @Override 66 public void show() { 67 System.out.println("冬天很冷"); 68 } 69 }; 70 //2.声明Season对象的属性:private final修饰 71 private final String seasonName; 72 private final String seasonDesc; 73 74 //3.私有化类的构造器,并给对象属性赋值 75 private Season1(String seasonName, String seasonDesc) { 76 this.seasonDesc = seasonDesc; 77 this.seasonName = seasonName; 78 79 } 80 81 // 4.其他诉求1:获取枚举类对象的属性 82 public String getSeasonName() { 83 return seasonName; 84 } 85 86 public String getSeasonDesc() { 87 return seasonDesc; 88 } 89 90 // @Override 91 // public String toString() { 92 // return "Season{" + 93 // "seasonName=‘" + seasonName + ‘‘‘ + 94 // ", seasonDesc=‘" + seasonDesc + ‘‘‘ + 95 // ‘}‘; 96 // } 97 98 }
以上是关于枚举类的主要内容,如果未能解决你的问题,请参考以下文章