Java 1.4 中枚举的替代方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1038321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:34:37  来源:igfitidea点击:

Alternative to enum in Java 1.4

javaenumsjava1.4

提问by Daniel Moura

Since Java 1.4 doesn't have enums I'm am doing something like this:

由于 Java 1.4 没有枚举,我正在做这样的事情:

public class SomeClass {
     public static int SOME_VALUE_1 = 0;
     public static int SOME_VALUE_2 = 1;
     public static int SOME_VALUE_3 = 2;

     public void receiveSomeValue(int someValue) {
            // do something
     }
 }

The caller of receiveSomeValue should pass one those 3 values but he can pass any other int. If it were an enum the caller could only pass one valid value.

receiveSomeValue 的调用者应该传递这 3 个值之一,但他可以传递任何其他 int。如果它是一个枚举,调用者只能传递一个有效值。

Should receiveSomeValue throw an InvalidValueException?

receiveSomeValue 应该抛出 InvalidValueException 吗?

What are good alternatives to Java 5 enums?

Java 5 枚举有哪些好的替代方案?

采纳答案by Roland Schneider

Best to use in pre 1.5 is the Typesafe Enum Patternbest described in the book Effective Java by Josh Bloch. However it has some limitations, especially when you are dealing with different classloaders, serialization and so on.

最好在 1.5 之前使用的类型安全枚举模式Josh Bloch 的 Effective Java一书中得到了最好的描述。但是它有一些限制,特别是当您处理不同的类加载器、序列化等时。

You can also have a look at the Apache Commons Lang project and espacially the enum class, like John has written. It is an implementation of this pattern and supports building your own enums.

您还可以查看 Apache Commons Lang 项目,尤其是 enum 类,如 John 所写。它是此模式的实现,并支持构建您自己的枚举。

回答by John Meagher

Apache Commons Lang has an Enum classthat works well and pretty well covers what Java 5 Enums offer.

Apache Commons Lang 有一个Enum 类,它运行良好,并且很好地涵盖了 Java 5 Enums 提供的内容。

回答by Nick Holt

I'd typically create what I call a constant class, some thing like this:

我通常会创建我所说的常量类,如下所示:

public class MyConstant 
{
  public static final MyConstant SOME_VALUE = new MyConstant(1);
  public static final MyConstant SOME_OTHER_VALUE = new MyConstant(2);
  ...

  private final int id;


  private MyConstant(int id)
  {
    this.id = id;
  }

  public boolean equal(Object object) 
  {
    ...
  }

  public int hashCode() 
  {
    ...
  }
}

where equalsand hashCodeare using the id.

在哪里equalshashCode正在使用id.

回答by bhavik shah

If the application code base is going to use lot of enums, then I would prefer following solution, which I have used in my application.

如果应用程序代码库将使用大量枚举,那么我更喜欢以下解决方案,该解决方案已在我的应用程序中使用。

Base Class

基类

public class Enum {
    protected int _enumValue;
    protected Enum(int enumValue) {
        this._enumValue = enumValue;
    }

    public int Value() {
        return this._enumValue;
    }
}

Your enumerations will then follow these pattern

然后您的枚举将遵循这些模式

Actual Enum

实际枚举

public class DATE_FORMAT extends Enum {
    public static final int DDMMYYYY = 1;
    public static final int MMDDYYYY = 2;
    public static final int YYYYMMDD = 3;

    public DATE_FORMAT(int enumValue) {
        super(enumValue);
    }
}

And your code can consume this enum as follows

您的代码可以按如下方式使用此枚举

String getFormattedDate(DATE_FORMAT format) {

    String sDateFormatted = "";

    switch (format.Value()) {

        case DATE_FORMAT.DDMMYYYY : 
            break;
        case DATE_FORMAT.MMDDYYYY :
            break;
        case DATE_FORMAT.YYYYMMDD :
            break;
        default:
            break;
    }

    return sDateFormatted;
}

Caller can use the function as

调用者可以使用该函数作为

void callerAPI() {
    DATE_FORMAT format = new DATE_FORMAT(DATE_FORMAT.DDMMYYYY);

    String sFormattedDate = getFormattedDate(format);

}

This is yet not full proof against intitializing derived Enum objects with any integer value. However it can provide good syntactic guideline to work in non-enum environment.

这还不是反对使用任何整数值初始化派生 Enum 对象的完整证据。然而,它可以为在非枚举环境中工作提供良好的语法指南。