2015-06-26 26 views
1

我正在将Java(JDK 1.5)“枚举”转换为Java ME(JDK 1.4)。Java ME:将基本JDK 1.5“枚举”对象转换为Java ME CLDC-1.1/IMP-NG(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]; 
    } 
} 
+1

这实际上取决于您在代码中依赖枚举的独特功能的程度。如果没有任何东西想要序列化或做任何事情,你可以用类来模拟枚举并实现你正在使用的特定功能。如果你不需要类型安全,你甚至可以决定用普通常量替换枚举。 – RealSkeptic

+0

我用普通常量替换所有东西可能会起作用。我会试着看看它是否有效。 TNX –

回答

2

这个1997年的文章展示了如何create enumerated constands在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"); 
}