【问题标题】:Java ME: transform basic JDK 1.5 "enum" object into Java ME CLDC-1.1/IMP-NG (JDK 1.4)Java ME:将基本的 JDK 1.5“枚举”对象转换为 Java ME CLDC-1.1/IMP-NG (JDK 1.4)
【发布时间】:2015-06-26 10:15:20
【问题描述】:

我正在将 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];
    }
}

【问题讨论】:

  • 这真的取决于你对代码中枚举的独特功能的依赖程度。如果没有尝试序列化或做任何花哨的事情,您可以使用类模拟枚举并实现您正在使用的特定功能。如果您不需要类型安全,您甚至可以决定用普通常量替换枚举。
  • 我用普通常量替换所有内容可能会起作用。我要试试看它是否有效。 Tnx

标签: java enums java-me jdk1.5 jdk1.4


【解决方案1】:

这篇 1997 年的文章展示了如何在 Java 中create enumerated constands

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

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");
}

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-11-20
    • 2011-12-26
    • 2015-09-01
    • 2011-02-18
    相关资源
    最近更新 更多