1

我正在将 Java(JDK 1.5)“枚举”翻译成 Java ME(JDK 1.4)。

很多人建议使用 retroweaver 将 JDK 1.5 库解析为 JDK 1.4,但我在使用它时遇到了很多问题,由于硬件限制,我真的想完全控制我的项目。

翻译它或找到等效的最佳方法是什么?

/** 
 Authentication enumerates the authentication levels.
*/
public enum Authentication
{
    /** 
     No authentication is used.
    */
    NONE,
    /** 
     Low authentication is used.
    */
    LOW,
    /** 
     High authentication is used.
    */
    HIGH,
    /*
     * High authentication is used. Password is hashed with MD5.
     */
    HIGH_MD5,
    /*
     * High authentication is used. Password is hashed with SHA1.
     */
    HIGH_SHA1,
    /*
     * High authentication is used. Password is hashed with GMAC.
     */
    HIGH_GMAC;

    /*
     * Get integer value for enum.
     */
    public int getValue()
    {
        return this.ordinal();
    }

    /*
     * Convert integer for enum value.
     */
    public static Authentication forValue(int value)
    {
        return values()[value];
    }
}
4

1 回答 1

2

这篇 1997 年的文章展示了如何在 Java中创建枚举常量。

这个想法是有一个带有私有构造函数和公共常量的最终类。使用的示例是:

public final class Color {

  private String id;
  public final int ord;
  private static int upperBound = 0;

  private Color(String anID) {
    this.id = anID;
    this.ord = upperBound++;
  }

  public String toString() {return this.id; }
  public static int size() { return upperBound; }

  public static final Color RED = new Color("Red");
  public static final Color GREEN = new Color("Green");
  public static final Color BLUE = new Color("Blue");
}
于 2015-06-30T10:48:07.157 回答