枚举类的使用
枚举类的理解
-
类的对象只有有限个,确定的。举例如下:
- 星期:Monday(星期一)、 …、 Sunday(星期天)
- 性别:Man(男)、Weman(女)
- 季节:Spring(春天)、 …、 Winter(冬天)
- 支付方式:Cash(现金)、WeChatPay(微信)、AliPay(支付宝)、BankCard(银行卡)、CreditCard(信用卡)
- 就职状态:Busy、Free、Vocation、Dimission
- 订单状态:Nonpayment(未付款)、Paid(已付款)、Delivered(已发货)、Return(退货)、Checked(已确认)Fulfilled(已配货)
- 线程状态:创建、就绪、运行、阻塞、死亡
-
当需要定义一组常量时,强烈建议使用枚举类
-
枚举类的实现
- jdk1.5之前需要自定义枚举类
- jdk1.5新增的enum关键字用于定义枚举类
-
若枚举只有一个对象,则可以作为一种单例模式的实现方式
自定义枚举类
枚举类的属性
- 枚举类对象的属性不应允许被改动,所以需要用private final来修饰
- 枚举类的使用private final修饰的属性应在构造器中为其赋值
- 若枚举类显示定义了带参数的构造器,则在列出枚举值的时也必须传入对应的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class SeasonTest { public static void main(String[] args) { Season spring = Season.SPRING; Season summer = Season.SUMMER; Season autumn = Season.AUTUMN; Season winter = Season.WINTER; System.out.println(spring); System.out.println(summer); System.out.println(autumn); System.out.println(winter); } }
class Season{ private final String seasonName; private final String seasonDesc; private Season(String seasonName,String seasonDesc){ this.seasonName=seasonName; this.seasonDesc=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("冬天","冰天雪地");
public String getSeasonName() { return seasonName; }
public String getSeasonDesc() { return seasonDesc; }
@Override public String toString() { return "enumText.Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
|
使用enum关键字定义枚举类
使用说明
- 使用enum定义的枚举类默认继承了java.lang.Enum类,因此不能再继承其他类
- 枚举类的构造器只能用private权限修饰符
- 枚举类的所有实例必须在枚举类中显示列出,以",“分隔,”;"结尾。列出的实例系统会自动添加public static final修饰
- 必须在枚举类的首行声明枚举类对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class SeasonTest1 { public static void main(String[] args) { Season1 summer = Season1.SUMMER;
System.out.println(summer); System.out.println(Season1.class.getSuperclass()); } }
enum Season1 { SPRING("春天", "春暖花开"), SUMMER("夏天", "烈日炎炎"), ANTUMN("秋天", "秋高气爽"), WINTER("冬天", "冰天雪地"); private final String seasonName; private final String seasonDesc; Season1(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 "Season1{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
|
注;Enum已经重写过toString()方法了 --> 得到当前枚举类常量的名称。
我们可以再次重写toString()方法,使得到的结果更易读
Enum类中的常用方法
方法 |
描述 |
values() |
返回枚举类型的对象数组。该方法可以很方便的遍历所有枚举值 |
valueOf(String objName) |
返回枚举类中对象名是objName的对象 |
toString() |
返回当前枚举类对象常量的名称 如果没有objName的枚举类对象,则抛异常IllegalArgumentException |
values()和toString()方法测试
1 2 3 4 5 6 7 8
| @Test public void test01() { Thread.State[] threads = Thread.State.values(); for (int i = 0; i < threads.length; i++) { System.out.println(threads[i].toString()); } }
|
valuesOf(String name)方法测试
1 2 3 4 5 6 7
| @Test public void test02(){ Season1 winter = Season1.valueOf("WINTER"); System.out.println(winter); }
|
使用enum关键字定义枚举类实现接口
使用enum关键字定义的枚举类实现接口的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @Test public void test03() { Sex[] sexes = Sex.values(); for (int i = 0; i < sexes.length; i++) { System.out.println(sexes[i]); sexes[i].show(); } }
interface Info { void show(); }
enum Sex implements Info { MAN("男人", "声音粗犷"), WEMAN("女人", "声音细腻"); private final String sex; private final String sound;
Sex(String sex, String sound) { this.sex = sex; this.sound = sound; }
public String getSex() { return sex; }
@Override public void show() { System.out.println("这是一个性别"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| @Test public void test04(){ Season1[] season = Season1.values(); for (int i = 0; i < season.length; i++) { System.out.println(season[i]); season[i].show(); } }
enum Season1 implements Info{
SPRING("春天", "春暖花开"){ @Override public void show() { System.out.println("这是春天,大地复苏"); } }, SUMMER("夏天", "烈日炎炎"){ @Override public void show() { System.out.println("这是夏天,热得要死"); } }, ANTUMN("秋天", "秋高气爽"){ @Override public void show() { System.out.println("这是秋天,硕果累累"); } }, WINTER("冬天", "冰天雪地"){ @Override public void show() { System.out.println("这是冬天,白雪皑皑"); } };
private final String seasonName; private final String seasonDesc;
Season1(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; }
public String getSeasonName() { return seasonName; }
public String getSeasonDesc() { return seasonDesc; } }
|
注解的使用
继续挖坑,以后再填
注解的理解
Annotation的使用示例
如何自定义注解
jdk中四个基本元注解的使用
利用反射获取注解信息
jdk8新特性:可重复注解
jkd8新特性:类型注解