`
lee79
  • 浏览: 106702 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

In prior releases, the standard way to represent an enumerated type was the int Enum pattern:

// int Enum Pattern - has severe problems!


public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL   = 3;

This pattern has many problems, such as:

  • Not typesafe - Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which makes no sense).
  • No namespace - You must prefix constants of an int enum with a string (in this case SEASON_ ) to avoid collisions with other int enum types.
  • Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
  • Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.

It is possible to get around these problems by using the Typesafe Enum pattern (see Effective Java Item 21), but this pattern has its own problems: It is quite verbose, hence error prone, and its enum constants cannot be used in switch statements.

In 5.0, the Java™ programming language gets linguistic support for enumerated types. In their simplest form, these enums look just like their C, C++, and C# counterparts:

enum Season { WINTER, SPRING, SUMMER, FALL }

But appearances can be deceiving. Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers. The new enum declaration defines a full-fledged class (dubbed an enum type ). In addition to solving all the problems mentioned above, it allows you to add arbitrary methods and fields to an enum type, to implement arbitrary interfaces, and more. Enum types provide high-quality implementations of all the Object methods. They are Comparable and Serializable , and the serial form is designed to withstand arbitrary changes in the enum type.

Here is an example of a playing card class built atop a couple of simple enum types. The Card class is immutable, and only one instance of each Card is created, so it need not override equals or hashCode :

import java.util.*;


public class Card {
    public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
        SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }

    public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }


    private final Rank rank;
    private final Suit suit;
    private Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() { return rank; }
    public Suit suit() { return suit; }
    public String toString() { return rank + " of " + suit; }

    private static final List<Card> protoDeck = new ArrayList<Card>();

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(new Card(rank, suit));
    }

    public static ArrayList<Card> newDeck() {
        return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
    }
}

The toString method for Card takes advantage of the toString methods for Rank and Suit . Note that the Card class is short (about 25 lines of code). If the typesafe enums (Rank and Suit ) had been built by hand, each of them would have been significantly longer than the entire Card class.

The (private) constructor of Card takes two parameters, a Rank and a Suit . If you accidentally invoke the constructor with the parameters reversed, the compiler will politely inform you of your error. Contrast this to the int enum pattern, in which the program would fail at run time.

Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.

The following example is a simple program called Deal that exercises Card . It reads two numbers from the command line, representing the number of hands to deal and the number of cards per hand. Then it creates a new deck of cards, shuffles it, and deals and prints the requested hands.

import java.util.*;

public class Deal {
    public static void main(String args[]) {
        int numHands = Integer.parseInt(args[0]);
        int cardsPerHand = Integer.parseInt(args[1]);
        List<Card> deck  = Card.newDeck();
        Collections.shuffle(deck);
        for (int i=0; i < numHands; i++)
            System.out.println(deal(deck, cardsPerHand));
    }

    public static ArrayList<Card> deal(List<Card> deck, int n) {
         int deckSize = deck.size();
         List<Card> handView = deck.subList(deckSize-n, deckSize);
         ArrayList<Card> hand = new ArrayList<Card>(handView);
         handView.clear();
         return hand;
     }
}

$ java Deal 4 5
[FOUR of HEARTS, NINE of DIAMONDS, QUEEN of SPADES, ACE of SPADES, NINE of SPADES]
[DEUCE of HEARTS, EIGHT of SPADES, JACK of DIAMONDS, TEN of CLUBS, SEVEN of SPADES]
[FIVE of HEARTS, FOUR of DIAMONDS, SIX of DIAMONDS, NINE of CLUBS, JACK of CLUBS]
[SEVEN of HEARTS, SIX of CLUBS, DEUCE of DIAMONDS, THREE of SPADES, EIGHT of CLUBS]

Suppose you want to add data and behavior to an enum. For example consider the planets of the solar system. Each planet knows its mass and radius, and can calculate its surface gravity and the weight of an object on the planet. Here is how it looks:

public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7),
    PLUTO   (1.27e+22,  1.137e6);

    private final double mass;   // in kilograms
    private final double radius; // in meters
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    public double mass()   { return mass; }
    public double radius() { return radius; }

    // universal gravitational constant  (m3
 kg-1
 s-2
)
    public static final double G = 6.67300E-11;

    public double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    public double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
}

The enum type Planet contains a constructor, and each enum constant is declared with parameters to be passed to the constructor when it is created.

Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):

    public static void main(String[] args) {
        double earthWeight = Double.parseDouble(args[0]);
        double mass = earthWeight/EARTH.surfaceGravity();
        for (Planet p : Planet.values())
           System.out.printf("Your weight on %s is %f%n",
                             p, p.surfaceWeight(mass));
    }

$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031

The idea of adding behavior to enum constants can be taken one step further. You can give each enum constant a different behavior for some method. One way to do this by switching on the enumeration constant. Here is an example with an enum whose constants represent the four basic arithmetic operations, and whose eval method performs the operation:

public enum Operation {
    PLUS, MINUS, TIMES, DIVIDE;

    // Do arithmetic op represented by this constant
    double eval(double x, double y){
        switch(this) {
            case PLUS:   return x + y;
            case MINUS:  return x - y;
            case TIMES:  return x * y;
            case DIVIDE: return x / y;
        }
        throw new AssertionError("Unknown op: " + this);
    }
}

This works fine, but it will not compile without the throw statement, which is not terribly pretty. Worse, you must remember to add a new case to the switch statement each time you add a new constant to Operation . If you forget, the eval method with fail, executing the aforementioned throw statement

There is another way give each enum constant a different behavior for some method that avoids these problems. You can declare the method abstract in the enum type and override it with a concrete method in each constant. Such methods are known as constant-specific methods. Here is the previous example redone using this technique:

public enum Operation {
  PLUS   { double eval(double x, double y) { return x + y; } },
  MINUS  { double eval(double x, double y) { return x - y; } },
  TIMES  { double eval(double x, double y) { return x * y; } },
  DIVIDE { double eval(double x, double y) { return x / y; } };

  // Do arithmetic op represented by this constant
  abstract double eval(double x, double y);
}

Here is a sample program that exercises the Operation class. It takes two operands from the command line, iterates over all the operations, and for each operation, performs the operation and prints the resulting equation:

    public static void main(String args[]) {
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        for (Operation op : Operation.values())
            System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));
    }

$ java Operation 4 2
4.000000 PLUS 2.000000 = 6.000000
4.000000 MINUS 2.000000 = 2.000000
4.000000 TIMES 2.000000 = 8.000000
4.000000 DIVIDE 2.000000 = 2.000000

Constant-specific methods are reasonably sophisticated, and many programmers will never need to use them, but it is nice to know that they are there if you need them.

Two classes have been added to java.util in support of enums: special-purpose Set and Map implementations called EnumSet and EnumMap . EnumSet is a high-performance Set implementation for enums. All of the members of an enum set must be of the same enum type. Internally, it is represented by a bit-vector, typically a single long . Enum sets support iteration over ranges of enum types. For example given the following enum declaration:

    enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

you can iterate over the weekdays. The EnumSet class provides a static factory that makes it easy:

    for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))
        System.out.println(d);

Enum sets also provide a rich, typesafe replacement for traditional bit flags:

    EnumSet.of(Style.BOLD, Style.ITALIC)

Similarly, EnumMap is a high-performance Map implementation for use with enum keys, internally implemented as an array. Enum maps combine the richness and safety of the Map interface with speed approaching that of an array. If you want to map an enum to a value, you should always use an EnumMap in preference to an array.

The Card class, above, contains a static factory that returns a deck, but there is no way to get an individual card from its rank and suit. Merely exposing the constructor would destroy the singleton property (that only a single instance of each card is allowed to exist). Here is how to write a static factory that preserves the singleton property, using a nested EnumMap :

private static Map<Suit, Map<Rank, Card>> table =
    new EnumMap<Suit, Map<Rank, Card>>(Suit.class);
static {
    for (Suit suit : Suit.values()) {
        Map<Rank, Card> suitTable = new EnumMap<Rank, Card>(Rank.class);
        for (Rank rank : Rank.values())
            suitTable.put(rank, new Card(rank, suit));
        table.put(suit, suitTable);
    }
}

public static Card valueOf(Rank rank, Suit suit) {
    return table.get(suit).get(rank);
}

The EnumMap (table ) maps each suit to an EnumMap that maps each rank to a card. The lookup performed by the valueOf method is internally implemented as two array accesses, but the code is much clearer and safer. In order to preserve the singleton property, it is imperative that the constructor invocation in the prototype deck initialization in Card be replaced by a call to the new static factory:

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(Card.valueOf(rank, suit)
);
    }

It is also imperative that the initialization of table be placed above the initialization of protoDeck , as the latter depends on the former. So when should you use enums? Any time you need a fixed set of constants. That includes natural enumerated types (like the planets, days of the week, and suits in a card deck) as well as other sets where you know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, and the like. It is not necessary that the set of constants in an enum type stay fixed for all time. The feature was specifically designed to allow for binary compatible evolution of enum types.

分享到:
评论

相关推荐

    enum类型重定义解决方法

    在编程中,`enum`(枚举)类型是用来定义一组命名的整数常量,它在C++中被广泛使用。然而,在某些情况下,我们可能会遇到`enum`类型的重定义问题,这通常发生在包含不同库或者头文件时,因为这些库可能已经定义了...

    C++中枚举类型(enum)

    ### C++中的枚举类型(Enum) 在C++编程语言中,枚举(`enum`)是一种用户定义的类型,它由一组具有整数值的命名常量组成。这些值可以被程序用来代替数字,使代码更具可读性,并且能够表示特定范围内的固定集合。 ...

    Java枚举类型Enum的用法

    Java枚举类型(Enum)是Java SE 5.0引入的一种新的数据类型,它为开发者提供了更为强大且安全的方式来表示一组常量。枚举在Java中不仅是一个类,还是一种特殊的类型,允许我们定义自己的常量集合。接下来,我们将...

    opc enum 32-64安装包

    OPC Enum 32-64 安装包是一款专为处理32位和64位操作系统设计的OPC组件,确保在不同系统环境下能稳定、高效地工作。 OPC Core Component是OPC技术的核心组成部分,它提供了基本的OPC服务,如数据访问(OPC DA,OPC ...

    WPF 中一组 RadioButton 向 Enum 的绑定

    其中一个常见的需求是将RadioButton组与枚举类型(Enum)进行绑定,以便用户通过选择不同的RadioButton来设置某个属性的值。枚举是一种强大的数据类型,它允许我们定义一组具有特定名称的常量,这些常量通常代表某种...

    enum 结构分析源码

    枚举(enum)在编程语言中是一种非常基础且重要的数据类型,它允许程序员定义一组预定义的常量,这些常量通常表示特定的值或状态。在这个“enum 结构分析源码”资源中,我们主要关注的是C语言中的枚举使用和实现。...

    enum类型被intent所携带时各种情况的示例代码

    然而,对于enum(枚举)类型,由于它不是Android系统默认支持的数据传递类型,所以在使用Intent传递enum时需要特别处理。本教程将详细讲解enum类型如何与Intent结合,以及在不同情况下如何实现数据传输。 首先,让...

    java enum 枚举 学习资料

    "Java Enum 枚举学习资料" Java Enum 枚举学习资料是 Java 编程语言中的一种特殊类型,它主要用来实现一组固定的常量。 Enum 枚举类型是 Java 5 中引入的一种新特性,旨在解决传统的 int 枚举常量的缺陷。 1. 枚举...

    Options:有时在某些情况下,您想在OptionSet中使用Enum或希望Enum由Raw的Int类型支持,但同时也具有String标签

    选项Swift软件包,用于更强大的Enum类型。 目录介绍特征安装用法设置一个MappedValueRepresentable枚举使用MappedValueCollectionRepresented 使用MappedEnum类型的可编码枚举在ESet中使用OptionSet中的EnumSet 将...

    Java enum的用法详解

    Java enum 的用法详解 Java enum(枚举)是一种特殊的数据类型,用于定义一组固定的常量。从 JDK 1.5 开始,Java 引入了 enum 机制,解决了传统的常量定义方式的不足。 用法一:常量 在 JDK 1.5 之前,我们定义...

    枚举语句enum用法详解

    枚举(enum)在编程语言中是一种非常有用的类型,它允许我们定义一组命名常量,这些常量通常代表一组相关的值。在C#中,枚举(enum)被广泛用于增强代码的可读性和可维护性。下面我们将深入探讨C#中的枚举用法。 一...

    Laravel开发-enum

    在Laravel框架中,"enum"(枚举)是一种实用的工具,用于限制特定属性或方法的值只能是预定义的一组。PHP在版本8.0中引入了对枚举的支持,这使得在 Laravel 开发中更加方便地处理固定选项的数据类型。本教程将深入...

    Enum汇总大全详细讲解

    枚举(Enum)在Java中是一种特殊的类,用于定义固定的常量集合,它提供了一种安全、类型化的常量表示方式。以下是对Enum的详细讲解: 1. **基本枚举定义**: ```java enum Color {BLUE, RED, GREEN;} ``` 这是...

    Laravel开发-laravel-enum

    在Laravel框架中,枚举(Enum)是一种实用的工具,可以帮助我们更好地组织代码,提高可读性和可维护性。Laravel Enum库是为Laravel应用程序设计的一个扩展,它允许开发者使用枚举类型来替代传统的常量或字符串,从而...

    Python库 | enum-0.4.7.tar.gz

    在这个场景中,我们关注的是一个名为"enum"的Python库,其版本为0.4.7,封装在"enum-0.4.7.tar.gz"的压缩包文件中。这个压缩包通常包含了库的所有源代码、文档和其他相关资源。 `enum`是Python中的一个内置库,自...

    Java Enum使用Demo源码

    在Java编程语言中,枚举(Enum)是一种特殊的类,用于定义固定的常量集合。它在许多场景下比常量接口或静态final变量更安全、更方便。本篇将通过"Java Enum使用Demo源码"深入探讨Java枚举的用法。 首先,枚举在Java...

    java enum 枚举的spring boot2.x完美实现demo源码

    在Java编程语言中,枚举(`enum`)是一种强大的工具,它允许我们定义一组预定义的常量。在Spring Boot 2.x框架中,枚举的使用可以帮助我们更好地管理和组织代码,提供类型安全和可读性。本篇将深入探讨如何在Spring ...

    PyPI 官网下载 | aenum-1.2.1.tar.gz

    **PyPI 官网下载 | aenum-1.2.1.tar.gz** 在Python编程语言中,`PyPI`(Python Package Index)是官方的软件仓库,它为开发者提供了存储和分享他们创建的Python模块、库和其他软件工具的平台。用户可以方便地通过`...

    DNS,ENUM原理介绍

    简单介绍了DNS和ENUM的原理及在IMS网络中的使用

Global site tag (gtag.js) - Google Analytics