简体   繁体   English

MIDP 2移动应用程序中的Java枚举

[英]Java enums in MIDP 2 mobile application

I've just got back to MIDP development after some 4 years of .NET 2 and Java 5 and 6. During that time I got to like using enums quite a lot. 我在.NET 2和Java 5和6之后的4年时间里回到了MIDP开发阶段。在那段时间里,我非常喜欢使用enum。

Enum is a language feature that allows a developer to have more confidence in some parts of his code, specially for being able to avoid or detect errors earlier (during compilation). Enum是一种语言功能,允许开发人员对代码的某些部分更有信心,特别是能够在之前(编译期间)避免或检测错误。 Some other advantages can be found here: http:// java.sun.com/j2se/1.5.0/docs/guide/language/enums.html 其他一些优点可以在这里找到: http:// java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

I found it strange that I couldn't find them in MIDP 2.0. 我觉得很奇怪,我在MIDP 2.0中找不到它们。 I've got this error message: 我有这个错误信息:

"Type 'enum' should not be used as an identifier, since it is a reserved keyword from source level 1.5" “类型'枚举'不应该用作标识符,因为它是源级别1.5的保留关键字”

I had some experience in Java 1.4 a while back, but I didn't remember this. 我曾经在Java 1.4中有过一些经验,但我不记得了。 There sure are some features of newer versions of your high-level languages that you get to take for granted... 肯定有一些新版本的高级语言的功能,你可以认为这是理所当然的......

Anyway, here is a good recommendation for what to do without them (if you're developing MIDP or dealing with code prior to Java 5): http:// www.javacamp.org/designPattern/enum.html 无论如何,如果没有它们(如果您正在开发MIDP或在Java 5之前处理代码),这里有一个很好的建议: http:// www.javacamp.org/designPattern/enum.html

Summing it up (for more details and a good explanation, follow the previous link. Many thanks to the original author): 总结一下(更多细节和一个很好的解释,请按照上一个链接。非常感谢原作者):

//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;
    }

}

Do you have any other different approaches to this issue? 你对这个问题有什么不同的方法吗?

The problem with MIDP is that it is stuck at Java language level 1.2 (some say 1.3 or 1.4 but thats not the point) and Enums were introduced in 1.5. MIDP的问题是它停留在Java语言级别1.2(有人说1.3或1.4,但这不是重点)和Enums是在1.5中引入的。 Your pattern is a step into the right direction, but lacks some features of "real" enums, like assigning an ordinal number to each constant. 您的模式是向正确方向迈出的一步,但缺少“真实”枚举的某些功能,例如为每个常量分配序数。

You might run into similar issues with generics, annotations, etc. which were also introduced in 1.5. 您可能会遇到类似于泛型,注释等的问题,这些问题也在1.5中引入。 There are tools to convert Java 1.5 back to 1.2, some are listed here . 有一些工具可以将Java 1.5转换回1.2,其中一些列在这里 Like that, you should be able to code in 1.5 and run on MIDP. 像这样,你应该能够在1.5中编码并在MIDP上运行。 Note, however, that using those tools will complicate your build process considerably, while the solution you mentioned does not. 但请注意,使用这些工具会使构建过程变得相当复杂,而您提到的解决方案却没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM