Java枚举类型重温

Corwien 2020/7/8 5:26:27

在JDK1.5 之前,我们定义常量都是: public static fianl.... 。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。 一. 什么是枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也…
在JDK1.5 之前,我们定义常量都是: public static fianl.... 。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。

一. 什么是枚举

枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景,如我们需要表达性别(男、女),颜色(红、黄、蓝),星期(星期一、星期二...星期日),四季(春、夏、秋、冬),地理位置(东、西、南、北),方向(前、后、左、右)等,这些场景都非常适合枚举。

二. 定义枚举

java中使用enum来定义枚举,和classinterface具有同样的等级,(注意是enum,而不是Enum),定义枚举可以有两种情况

第一种:默认构造器(空构造器)

public enum Quarter {
SPRING, SUMMER, AUTUMN, WINTER;
}

未定义成员变量和成员方法,省略了 private Quarter() {}

public enum Quarter {
SPRING, SUMMER, AUTUMN, WINTER;
private Quarter(){}
}

第二种:定义了成员变量、带参构造器

枚举可以定义成员变量,包括成员属性、方法,抽象方法,静态方法等

public enum Quarter {
SPRING("春"), SUMMER("夏"), AUTUMN("秋"), WINTER("冬");

private Quarter(String name){
this.name = name;
}
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
//静态方法
public static void printName(){
System.out.println(Quarter.SUMMER);
}

//抽象方法
public abstract void printValue();
}

注意:枚举和类一样,可以有多个构造器,即有了一个带参构造器,还可以有无参构造器,编译是可以通过的。

除此之外:

  • 1.枚举enum 默认继承了java.lang.Enum类,实现了 java.lang.Seriablizable 和 java.lang.Comparable 两个接口,可序列化以及进行比较;
  • 2.所有的枚举值都是常量,默认采用了public static final 进行了修饰,enum不是类,自然也不能被继承或实现;
  • 3.枚举值必须在第一行,否则编译出错;

三. 枚举常见用法

1.获取枚举元素以及成员变量

public static void main(String[] args) {
//获取枚举元素
System.out.println(Quarter.SPRING);
//调用toString()方法将枚举元素转化为String类型
System.out.println(Quarter.SPRING.name());
System.out.println(Quarter.SPRING.toString());
//获取成员变量
System.out.println(Quarter.SPRING.getName());
}

输出结果:

SPRING  
SPRING  
SPRING  
春

2.枚举遍历

有时候我们需要将枚举元素都取出来作为查询条件,此时就需要进行遍历,通过调用Quarter.values()方法

public static void main(String[] args) {
//通过values()方法得到Quarter元素的数组
Quarter[] quarters = Quarter.values();
List<String> quarterParam = new ArrayList<>(quarters.length);
//将数组中的元素存储到List集合quarterParam中
for (Quarter quarter : quarters) {
quarterParam.add(quarter.toString());
}
}

3.switch条件判断

枚举的元素有限且固定,通过switch做条件判断却是正好

public static void main(String[] args){
Quarter quarter = Quarter.AUTUMN;
switch (quarter) {
case SPRING:
System.out.println("找到了,是它:" + SPRING);
break;
case SUMMER:
System.out.println("找到了,是它:" + SUMMER);
break;
case AUTUMN:
System.out.println("找到了,是它:" + AUTUMN);
break;
case WINTER:
System.out.println("找到了,是它:" + WINTER);
break;
default:
System.out.println("未找到");
break;
}
}

执行后结果:

找到了,是它:AUTUMN
简单说明一下:其实此处不需要default的内容,或者说将WINTER作为default即可,因为枚举天然有类型限制,你只能传递它已有的元素以及null,不过正常情况下都会提前判断传递的参数是否为null,当switch接收到null时会报java.lang.NullPointerException。

4.枚举比较

4.1. 判断是否相等

枚举判断两个元素是否相等直接使用==进行判断即可,因为它非类,也无法实例化,存储位置自然也不会根据对象的不同而不同。

public static void main(String[] args){
System.out.println(Quarter.AUTUMN == Quarter.AUTUMN);
System.out.println(Quarter.AUTUMN == Quarter.WINTER);
}

执行结果:

true  
false

也可以通过equals()方法进行比较,不过没必要,因为其底层也是通过 == 来实现的。

public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
//equals比较
public final boolean equals(Object other) {
return this==other;
}
}

4.2. 顺序比较

通过compareTo进行枚举元素比较,此处比较的是在枚举中元素的先后顺序,返回的是位置序数的差值

public static void main(String[] args){
System.out.println(Quarter.AUTUMN.compareTo(Quarter.SPRING));
System.out.println(Quarter.AUTUMN.compareTo(Quarter.WINTER));
}

执行结果为:

2  
-1

我们看一下compareTo方法

public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable {
//元素在枚举中的序号
private final int ordinal;
//可以看到compareTo方法返回的就是两个元素ordinal的差值
public final int compareTo(E o) {
Enum<?> other = (Enum<?>)o;
Enum<E> self = this;
if (self.getClass() != other.getClass() &&
self.getDeclaringClass() != other.getDeclaringClass())
throw new ClassCastException();
return self.ordinal - other.ordinal;
}
}

5.枚举也可也实现接口

public interface WeatherInterface {
  //获取温度
  public String getTemperature(Quarter quarter);
}
public enum Quarter implements WeatherInterface{
SPRING("春"), SUMMER("夏"), AUTUMN("秋"), WINTER("冬");

private Quarter(String name){
this.name = name;
}

private String name;

public String getName() {
return name;
}
//重写获取温度方法
@Override
public String getTemperature(Quarter quarter) {
switch (quarter) {
case SPRING:
return "适中";
case SUMMER:
return "热";
case AUTUMN:
return "凉爽";
case WINTER:
return "寒冷";
default:
return "无法确定";
}
}
}

但注意枚举不可继承其他类

public class Pquarter {}
//无法编译通过
public enum Quarter extends Pquarter {}

6.使用接口来组织枚举

如果枚举太多也不好管理,同时条理不够清晰,我们可以通过接口来将多个相关枚举组织在一起进行管理,

public interface Weather {
enum Quarter implements Weather {
SPRING, SUMMER, AUTUMN, WINTER;
}
enum Temperature implements Weather {
MODERATE, HEAT, COOL, COLD
}
//调用时直接通过Weather.Quarter.SPRING即可。
}

四.实战

Quarter.java 单个参数枚举类

package cn.how2j;

/**
 * Created by kaiyiwang on 20/7/7.
 */
public enum Quarter {
  SPRING("春"), SUMMER("夏"), AUTUMN("秋"), WINTER("冬");

  private Quarter(String name){
    this.name = name;
  }

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  //静态方法
  public static void printName(){
    System.out.println(Quarter.SUMMER);
  }


}

两个参数枚举类:QuarterTwo.java

package cn.how2j;

/**
 * Created by kaiyiwang on 20/7/7.
 */
public enum QuarterTwo {
  SPRING("春", 1), SUMMER("夏", 2), AUTUMN("秋", 3), WINTER("冬", 4);

  private QuarterTwo(String name, int value){
    this.name = name;
    this.value = value;
  }

  private String name;
  private int value;

  public String getName() {
    return name;
  }

  public int getValue() {
    return value;
  }

  public void setName(String name) {
    this.name = name;
  }
  public void setValue(int value) {
    this.value = value;
  }

  //静态方法
  public static void printName(){
    System.out.println(QuarterTwo.SUMMER);
  }


}

测试类 TestEnum.java

package cn.how2j;


/**
 * Created by kaiyiwang on 20/7/7.
 */
public class TestEnum {

  public static void main(String[] args) {
    // 获取枚举元素
    System.out.println(Quarter.SPRING);

    //调用toString()方法将枚举元素转化为String类型
    System.out.println(Quarter.SPRING.name());
    System.out.println(Quarter.SPRING.toString());

    //获取成员变量
    System.out.println(Quarter.SPRING.getName());

    // 多参数构建
    System.out.println("-------- TWO --------");
    System.out.println(QuarterTwo.SPRING);
    System.out.println(QuarterTwo.SPRING.name());
    System.out.println(QuarterTwo.SPRING.getName());
    System.out.println(QuarterTwo.SPRING.getValue());
    
  }

}

打印结果:

/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 15.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar:/Users/kaiyiwang/Code/java/rabbitmq_fanout/target/classes:/WEB/java/maven/repository/com/rabbitmq/amqp-client/3.6.5/amqp-client-3.6.5.jar:/WEB/java/maven/repository/cn/hutool/hutool-all/4.3.1/hutool-all-4.3.1.jar:/Applications/IntelliJ IDEA 15.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain cn.how2j.TestEnum
SPRING
SPRING
SPRING
春
-------- TWO --------
SPRING
SPRING
春
1

Process finished with exit code 0

其他:

enum MallSalesOrderStatus{
    
    UNKNOW("未知", 0),
    WAIT_BUYER_PAY("等待买家支付", 10),
    WAIT_BUYER_CONFIRM_GOODS("等待买家确认收货", 30),
    REFUND_FINISHED("退款完成", 60);
    
    private final String caption;
    private final int value;
    
    MallSalesOrderStatus(String caption, int value){
        this.caption = caption;
        this.value = value;
    }
    
}

public class Test{

  public static void main(String[] args)
  
  {
     MallSalesOrderStatus status = MallSalesOrderStatus.WAIT_BUYER_PAY;
      
      System.out.print(status);
  
  }

}

五. 总结

本文主要介绍了枚举enum主要特点,定义,无参构造器,有参构造器,枚举的常用方法举例以及主要的应用场景。


转载文章:
Java的枚举enum示例详解

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[Java枚举类型重温]http://www.zyiz.net/tech/detail-142696.html

上一篇:Spring 源码第四弹!深入理解 BeanDefinition

下一篇:我从LongAdder中窥探到了高并发的秘籍,上面只写了两个字...

赞(0)

共有 条评论 网友评论

验证码: 看不清楚?
    关注微信小程序
    程序员编程王-随时随地学编程

    扫描二维码或查找【程序员编程王】

    可以随时随地学编程啦!

    技术文章导航 更多>
    扫一扫关注最新编程教程