今天我浏览了这个网站上的一些问题,我发现一个枚举被用于单例模式,据称线程安全的好处,这种解决方案。

我从来没用过枚举,而且我已经用Java编程好几年了。显然,他们改变了很多。现在他们甚至在自己内部完全支持OOP。

为什么我应该在日常编程中使用enum ?


当前回答

我会使用枚举作为有用的映射工具,避免使用多个if-else 前提是实现了一些方法。

public enum Mapping {

    ONE("1"),
    TWO("2");

    private String label;

    private Mapping(String label){
        this.label = label;
    }

    public static Mapping by(String label) {

        for(Mapping m: values() {
            if(m.label.equals(label)) return m;
        }

        return null;
    }

}

因此by(String label)方法允许您通过非枚举获取枚举值。此外,还可以创建两个枚举之间的映射。还可以尝试“1对多”或“多对多”,除了“一对一”的默认关系

最后,enum是一个Java类。你可以在里面有main方法,这在需要马上对参数做一些映射操作时很有用。

其他回答

除了@BradB,回答:

That is so true... It's strange that it is the only answer who mention that. When beginners discover enums, they quickly take that as a magic-trick for valid identifier checking for the compiler. And when the code is intended to be use on distributed systems, they cry... some month later. Maintain backward compatibility with enums that contains non static list of values is a real concern, and pain. This is because when you add a value to an existing enum, its type change (despite the name does not).

"Ho, wait, it may look like the same type, right? After all, they’re enums with the same name – and aren’t enums just integers under the hood?" And for these reasons, your compiler will likely not flag the use of one definition of the type itself where it was expecting the other. But in fact, they are (in most important ways) different types. Most importantly, they have different data domains – values that are acceptable given the type. By adding a value, we’ve effectively changed the type of the enum and therefore break backward compatibility.

总之:当你想使用它的时候使用它,但是,请检查所使用的数据域是一个有限的、已知的、固定的集合。

枚举以自记录的方式枚举一组固定的值。 它们使您的代码更显式,也更不容易出错。

为什么不使用String,或int,而不是Enum常量?

编译器不允许错别字,也不允许出值的修正 设置,因为枚举本身就是类型。后果: 你不需要写一个先决条件(或手册)来确保你的论点在有效范围内。 类型不变式是免费的。 枚举可以有行为,就像任何其他类一样。 无论如何,您可能需要类似数量的内存来使用string(这取决于Enum的复杂性)。

此外,Enum的每个实例都是一个类,您可以为其定义单独的行为。

另外,它们在创建实例时(当枚举被加载时)确保线程安全,这在简化单例模式中有很好的应用。

这篇博客说明了它的一些应用,比如解析器的状态机。

使用枚举类型安全,这是一个语言特性,所以你通常会得到:

编译器支持(立即查看类型问题) ide中的工具支持(在切换情况下自动补全,缺失情况,强制默认,…) 在某些情况下,枚举的性能也很好(EnumSet,类型安全的替代传统的基于int的“位标志”)。

枚举可以有方法、构造函数,甚至可以在枚举中使用枚举,并将枚举与接口结合使用。

可以将枚举看作是替换一组定义良好的int常量的类型(Java从C/ c++“继承”了int常量),在某些情况下还可以替换位标志。

《Effective Java 2nd Edition》一书中有一整章是关于它们的,并且有更多的细节。也可以参考Stack Overflow的文章。

枚举?为什么要使用它?我认为当你使用它的时候会更容易理解。我也有同样的经历。

假设您有一个创建、删除、编辑和读取数据库操作。

现在如果你创建一个枚举作为一个操作:

public enum operation {
    create("1")
    delete("2")
    edit("3")
    read("4")

    // You may have is methods here
    public boolean isCreate() {
        return this.equals(create);
    }
    // More methods like the above can be written

}

现在,你可以这样声明:

private operation currentOperation;

// And assign the value for it 
currentOperation = operation.create

所以你可以在很多方面使用它。对于特定的事情使用enum总是好的,因为上面例子中的数据库操作可以通过检查currentOperation来控制。也许有人会说,这也可以用变量和整数值来实现。但我相信Enum更安全,更适合程序员。

另一件事:我认为每个程序员都喜欢布尔,不是吗?因为它只能存储两个值,两个特定的值。所以Enum可以被认为具有相同类型的功能,用户可以定义它将存储多少和什么类型的值,只是方式略有不同。:)

枚举就像类。与类一样,它也有方法和属性。

不同阶级的区别是: 1. 枚举常量是public, static, final。 2. 枚举不能用于创建对象,也不能扩展其他类。但是它可以实现接口。