enum 末蓝、 2023-05-28 14:18 4阅读 0赞 > [https://yuanyu.blog.csdn.net/article/details/105015326][https_yuanyu.blog.csdn.net_article_details_105015326] -------------------- > 枚举的应用场景为类的对象有限个 > > * 星期: Monday(星期一) ... Sunday(星期天) > * 性别: Man(男)、Woman(女) > * 季节: Spring(春节) ... Winter(冬天) > * 支付方式: Cash(现金)、WeChatPay(微信)、Alipay(支付宝)、BankCard(银行卡)、CreditCard(信用卡) > * 就职状态: Busy、Free、Vocation、Dimission > * 订单状态: Nonpayment(未付款)、Paid(已付款)、Delivered(已发货)、Return(退货)、Checked(已确认)、Fulfilled(已配货) > * 线程状态:创建、就绪、运行、阻塞、死亡 > jdk5.0之前需要自定义枚举类 > > * java.util.logging.Level /** * 自定义枚举类 */ public class Season{ //1.声明Season对象的属性(private final修饰) private final String name; private final String desc; //2.私有化类的构造器,并给对象属性赋值 private Season(String seasonName,String seasonDesc){ this.name = seasonName; this.desc = seasonDesc; } //3.提供当前枚举类的多个对象(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("冬天","冰天雪地"); //4. 获取枚举类对象的属性 public String getName(){return name;} public String getDesc(){return desc;} //4. 提供toString() @Override public String toString() { return "Season{"+"seasonName='"+name+'\''+", seasonDesc='"+desc+'\''+'}'; } } @Test public void testSeason(){ Season spring = Season.SPRING; System.out.println(spring.getName()); } > jdk5.0后可以使用enum关键字定义枚举类 <table> Enum类的主要方法 <thead> <tr> <th style="width:177px;"> <p>方法名</p> </th> <th>详细描述</th> </tr> </thead> <tbody> <tr> <td style="width:177px;">equals</td> <td> <p>在枚举类型中可以直接使用 “==” 来比较两个枚举常量是否相等;Enum提供的这个equals()方法,也是直接使用 “==” 实现的。它的存在是为了在Set、List和Map中使用;注意equals()是不可变的;</p> </td> </tr> <tr> <td style="width:177px;">hashCode</td> <td> <p>Enum实现了hashCode()来和equals()保持一致;它也是不可变的;</p> </td> </tr> <tr> <td style="width:177px;">getDeclaringClass</td> <td> <p>得到枚举常量所属枚举类型的Class对象;可以用它来判断两个枚举常量是否属于同一个枚举类型;</p> </td> </tr> <tr> <td style="width:177px;">name</td> <td> <p>得到当前枚举常量的名称;建议优先使用toString();</p> </td> </tr> <tr> <td style="width:177px;">ordinal</td> <td> <p>得到当前枚举常量的次序;</p> </td> </tr> <tr> <td style="width:177px;">compareTo</td> <td> <p>枚举类型实现了Comparable接口,这样可以比较两个枚举常量的大小(按照声明的顺序排列);</p> </td> </tr> <tr> <td style="width:177px;">clone</td> <td> <p>枚举类型不能被Clone;为了防止子类实现克隆方法,Enum实现了个仅抛出CloneNotSupportedException异常的不变Clone();</p> </td> </tr> <tr> <td style="width:177px;"><strong><span style="color:#f33b45;">values()</span></strong></td> <td>返回枚举类型的对象数组;该方法可以很方便地遍历所有的枚举值;<br> </td> </tr> <tr> <td style="width:177px;"><strong><span style="color:#f33b45;">valueOf(String str)</span></strong><br> </td> <td>可以把一个字符串转为对应的枚举类对象;要求字符串必须是枚举类对象的 “名字” ;如不是会有运行时异常IllegalArgumentException;</td> </tr> <tr> <td style="width:177px;"><strong><span style="color:#f33b45;">toString()</span></strong></td> <td> <p>得到当前枚举常量的名称;你可以通过重写这个方法来使得到的结果更易读;</p> </td> </tr> </tbody> </table> > 定义的枚举类默认继承于java.lang.Enum类 public enum Season{ //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束 SPRING("春天", "春暖花开") , SUMMER("夏天", "夏日炎炎") , AUTUMN("秋天", "秋高气爽"), WINTER("冬天", "冰天雪地"); //2.声明Season对象的属性(private final修饰) private final String name; private final String desc; //2.私有化类的构造器,并给对象属性赋值 (默认private) Season(String seasonName, String seasonDesc) { this.name = seasonName; this.desc = seasonDesc; } //4.获取枚举类对象的属性 public String getName(){return name;} public String getDesc(){return desc;} //@Override //public String toString() { // return "Season{"+"name='"+name+'\''+", desc='"+desc+'\''+'}'; //} } @Test public void testToString() { Season summer = Season.SUMMER; //toString():返回枚举类对象的名称 System.out.println(summer.toString());//SUMMER System.out.println(Season.class.getSuperclass());//class java.lang.Enum } @Test public void testValues() { //values():返回所有的枚举类对象构成的数组 Season[] seasons = Season.values(); Stream.of(seasons).forEach(System.out::println); System.out.println("****************"); Thread.State[] states = Thread.State.values(); Stream.of(states).forEach(System.out::println); } @Test public void testValueOf() { //valueOf(String objName):返回枚举类中对象名是objName的对象 Season winter = Season.valueOf("WINTER"); //winter 不可 System.out.println(winter); //如果没有objName的枚举类对象,则抛异常:IllegalArgumentException Season spring = Season.valueOf("NULL"); } > public interface Info{ void show(); } public enum Season implements Info { SPRING("春天", "春暖花开") { @Override public void show() { System.out.println("春天在哪里?"); } }, SUMMER("夏天", "夏日炎炎") { @Override public void show() { System.out.println("宁夏"); } }, AUTUMN("秋天", "秋高气爽") { @Override public void show() { System.out.println("秋天不回来"); } }, WINTER("冬天", "冰天雪地") { @Override public void show() { System.out.println("大约在冬季"); } }; private final String name; private final String desc; Season(String seasonName, String seasonDesc) { this.name = seasonName; this.desc = seasonDesc; } public String getName(){return name;} public String getDesc(){return desc;} //没有重写就是使用的这个 //@Override //public void show() { // System.out.println("这是一个季节"); //} } java.util.concurrent.TimeUnit java.lang.Thread.State [https_yuanyu.blog.csdn.net_article_details_105015326]: https://yuanyu.blog.csdn.net/article/details/105015326
还没有评论,来说两句吧...