你必定须要知道的高阶JAVA枚举特性!

JAVA枚举,比你想象中还要有用!java

我常常发现本身在Java中使用枚举来表示某个对象的一组潜在值。git

在编译时肯定类型能够具备什么值的能力是一种强大的能力,它为代码提供告终构和意义。github

当我第一次了解枚举时,当时我认为它们只是一个为常量命名的工具,能够很容易地被静态常量字符串ENUM_VAL_NAME所取代。编程

后来我发现我错了。事实证实,Java枚举具备至关高级的特性,可使代码干净、不易出错,功能强大。安全

让咱们一块儿来看看Java中的一些高级枚举特性,以及如何利用这些特性使代码更简单、更可读。app

枚举是类!

在Java中,枚举是Object的一个子类。让咱们看看全部枚举的基类,Enum<E>(为简洁起见进行了修改)。ide

public abstract class Enum<E extends Enum<E>>
    implements Constable, Comparable<E>, Serializable {
  private final String name;
  
  public final String name() {
      return name;
  }
  
  private final int ordinal;
  
  public final int ordinal() {
      return ordinal;
  }
  
  protected Enum(String name, int ordinal) {
      this.name = name;
      this.ordinal = ordinal;
  }
  
  public String toString() {
      return name;
  }
  
  public final boolean equals(Object other) {
      return this==other;
  }
  
  public final int hashCode() {
      return super.hashCode();
  }
  
  public final int compareTo(E o) {
      Enum<?> other = (Enum<?>)o;
      Enum<E> self = this;
      if (self.getClass() != other.getClass() && // optimization
          self.getDeclaringClass() != other.getDeclaringClass())
          throw new ClassCastException();
      return self.ordinal - other.ordinal;
  }
}

咱们能够看到,这基本上只是一个常规的抽象类,有两个字段,name和ordinal。函数式编程

因此说枚举都是类,因此它们具备常规类的许多特性。 函数

咱们可以为枚举提供实例方法、构造函数和字段。咱们能够重写toString(),但不能重写hashCode()或equals(Object other)。工具

接下来咱们看下咱们的枚举示例,Operation

enum Operation {
    ADD,
    SUBTRACT,
    MULTIPLY
  }

这个枚举表示一个Operation能够对两个值执行,并将生成一个结果。关于如何实现此功能,您最初的想法多是使用switch语句,以下所示:

public int apply(Operation operation, int arg1, int arg2) {
    switch(operation) {
      case ADD:
        return arg1 + arg2;
      case SUBTRACT:
        return arg1 - arg2;
      case MULTIPLY:
        return arg1 * arg2;
      default:
        throw new UnsupportedOperationException();
  }
}

固然,这样子会有一些问题。

第一个问题是,若是咱们将一个新操做添加到咱们的枚举Operation中,编译器不会通知咱们这个开关不能正确处理新操做。

更糟糕的是,若是一个懒惰的开发人员在另外一个类中复制或从新编写这些代码,咱们可能没法更新它。

第二个问题是默认状况default,每段程序里面都是必需的,尽管咱们知道在正确的代码里它永远不会发生。

这是由于Java编译器知道上面的第一个问题,而且但愿确保咱们可以处理在不知情的状况下向Operation中添加了新枚举。

还好,Java8用函数式编程为咱们提供了一个干净的解决方案。

函数枚举实现

由于枚举是类,因此咱们能够建立一个枚举字段来保存执行操做的函数。

可是在咱们找到解决方案以前,让咱们先来看看一些重构。

首先,让咱们把开关放在enum类中。

enum Operation {
  ADD,
  SUBTRACT,
  MULTIPLY;
  
  public static int apply(Operation operation, int arg1, int arg2) {
    switch(operation) {
      case ADD:
        return arg1 + arg2;
      case SUBTRACT:
        return arg1 - arg2;
      case MULTIPLY:
        return arg1 * arg2;
      default:
        throw new UnsupportedOperationException();
    }
  }
}

咱们能够这样作:Operation.apply(Operation.ADD, 2, 3);

由于咱们如今从Operation中调用方法,因此咱们能够将其更改成实例方法并使用this,而不是用Operation.apply()来实现,以下所示:

public int apply(int arg1, int arg2) {
  switch(this) {
    case ADD:
      return arg1 + arg2;
    case SUBTRACT:
      return arg1 - arg2;
    case MULTIPLY:
      return arg1 * arg2;
    default:
      throw new UnsupportedOperationException();
  }
}

像这样使用:Operation.ADD.apply(2, 3);

看起来变好了。如今让咱们更进一步,经过使用函数式编程彻底消除switch语句。

enum Operation {
              ADD((x, y) -> x + y),
              SUBTRACT((x, y) -> x - y),
              MULTIPLY((x, y) -> x * y);
  
              Operation(BiFunction<Integer, Integer, Integer> operation) {
                      this.operation = operation;
              }
  
              private final BiFunction<Integer, Integer, Integer> operation;
  
              public int apply(int x, int y) {
                      return operation.apply(x, y);
              }
  
  }

这里我作的是:

  • 添加了一个字段 BiFunction<Integer, Integer, Integer> operation
  • 用BiFunction建立了用于Operation的构造函数。
  • 调用枚举定义中的构造函数,并用lambda指定BiFunction<Integer, Integer, Integer>。

这个java.util.function.BiFunction operation字段是对采用两个参数的函数(方法)的引用。

在咱们的例子中,两个参数都是int型,返回值也是int型。不幸的是,Java参数化类型不支持原语,因此咱们必须使用Integer。

由于BiFunction是用@functioninterface注释的,因此咱们可使用Lambda表示法定义一个。

由于咱们的函数接受两个参数,因此咱们可使用(x,y)来指定它们。

而后咱们定义了一个单行方法,它使用 ->x+y 返回一个值。这至关于下面的方法,只是更简洁而已。

class Adder implements BiFunction<Integer, Integer, Integer> {
          @Override
          public Integer apply(Integer x, Integer y) {
                  return x + y;
    }
  }

咱们的新Operation实现采用相同的方式:Operation.ADD.apply(2, 3);.

可是,这种实现更好,由于编译器会告诉咱们什么时候添加了新Operation,这要求咱们更新新函数。若是没有这一点,若是咱们在添加新Operation时还不记得更新switch语句,就有可能获得UnsupportedOperationException()。

关键要点

  • Enum枚举是Enum<T>的扩展类。
  • Enum枚举能够有字段、构造函数和实例方法。
  • Enum枚举字段能够存储函数。与lambdas配合使用,能够建立干净、安全的特定于枚举的函数实现,并在编译时强制执行它们(而不是使用switch)。

下面是这个示例的GitHub地址。(https://github.com/alex-power...

本文参考:https://medium.com/javarevisi...

欢迎关注个人公众号:程序猿DD,得到独家整理的免费学习资源助力你的Java学习之路!另每周赠书不停哦~
相关文章
相关标签/搜索