2015-06-26 28 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"); 
}