在使用了 4 年的 .NET 2 和 Java 5 和 6 之后,我刚刚回到 MIDP 开发。在那段时间里,我非常喜欢使用枚举。
枚举是一种语言特性,它允许开发人员对其代码的某些部分更有信心,特别是能够更早地避免或检测错误(在编译期间)。其他一些优点可以在这里找到:http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
我发现在 MIDP 2.0 中找不到它们很奇怪。我收到此错误消息:
“类型 'enum' 不应用作标识符,因为它是源级别 1.5 中的保留关键字”
前段时间我有一些使用 Java 1.4 的经验,但我不记得了。你的高级语言的新版本肯定有一些你认为理所当然的特性......
无论如何,这里有一个很好的建议,没有它们怎么办(如果您正在开发 MIDP 或处理 Java 5 之前的代码):http://www.javacamp.org/designPattern/enum.html
总结一下(更多细节和很好的解释,请点击前面的链接。非常感谢原作者):
//The typesafe enum pattern
public class Suit {
private final String name;
public static final Suit CLUBS =new Suit("clubs");
public static final Suit DIAMONDS =new Suit("diamonds");
public static final Suit HEARTS =new Suit("hearts");
public static final Suit SPADES =new Suit("spades");
private Suit(String name){
this.name =name;
}
public String toString(){
return name;
}
}
您对这个问题还有其他不同的方法吗?