java 枚举有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11393389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:54:58  来源:igfitidea点击:

What is Enum useful for?

javaenums

提问by Roni Copul

After reading some questions and answers about enum I don't find it really useful...

在阅读了有关 enum 的一些问题和答案后,我发现它并没有真正有用...

It's something between class and a variable but I know where can I use it so it would be more useful then a class, or a couple of variables for example.

它介于类和变量之间,但我知道我可以在哪里使用它,因此它比类或几个变量更有用。

回答by Thor84no

There are other things you can do and reasons to use them, but the primary reason I use enums is to prevent the possibility of an invalid parameter. For example, imagine the following method:

您还可以做其他事情以及使用它们的原因,但我使用枚举的主要原因是为了防止出现无效参数的可能性。例如,想象以下方法:

public void doSomethingWithAMonth(int monthNum);

This is not only ambiguous (do month indexes start at 1, or at 0?), but you can give down-right invalid data (13+, negative numbers). If you have an enum Monthwith JAN, FEB, etc. the signature becomes:

这不仅含糊不清(月份索引是从 1 开始还是从 0 开始?),而且您可以提供完全无效的数据(13+,负数)。如果您有一个Month带有 JAN、FEB 等的枚举,则签名变为:

public void doSomethignWithAMonth(Month month);

Code calling this method will be far more readable, and can't provide invalid data.

调用此方法的代码将更具可读性,并且不能提供无效数据。

回答by ant

I use Enums for anything that has multiple nonchanging "type" of something. i.e days of a week.

我将枚举用于任何具有多个不变“类型”的东西。即一周中的几天。

Why I don't use String in this case is because I can't do switch statement on the String and I can on Enum so for every day of a week I can do something specific.

为什么在这种情况下我不使用 String 是因为我不能在 String 上执行 switch 语句,而我可以在 Enum 上执行,所以在一周的每一天我都可以做一些特定的事情。

I also use Enum when working with singleton pattern, Check the Effective Java book CH2(CHAPTER 2 CREATING AND DESTROYING OBJECTS), quote :

我在使用单例模式时也使用 Enum,检查Effective Java book CH2(CHAPTER 2 CREATING AND DESTROYING OBJECTS),引用:

"While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton." not gonna paste code read the book it's excellent.

“虽然这种方法尚未被广泛采用,但单元素枚举类型是实现单例的最佳方式。” 不会粘贴代码阅读这本书很棒。

If I were you I'd firstly read Thinking in javachapter Enumerated Types. Then after that Effective Java chapter 6 and you should be good

如果我是你,我会首先阅读Java章节枚举类型中的思考。然后在Effective Java第6章之后你应该会很好

回答by Pramod Kumar

Simplest Definition - An enum is just like any other class, with a predefined set of instances.

最简单的定义—— An enum is just like any other class, with a predefined set of instances.

Best part of enum is it can be used in switch statements where we can't use Strings.

枚举最好的部分是它可以用在我们不能使用字符串的 switch 语句中。

and Enum is a safe-type so you can't assign a new value at the runtime.

并且 Enum 是一种安全类型,因此您无法在运行时分配新值。

回答by EngineerBetter_DJ

An enumforces coders to select from a pre-defined list of entities or concepts. Whilst you could achieve the same objective with classes or setting a variable to a particular known value, an enumeration is less error-prone as it advertises and enforces a menu from which the setter of a value can pick.

Anenum强制编码人员从预定义的实体或概念列表中进行选择。虽然您可以使用类或将变量设置为特定的已知值来实现相同的目标,但枚举不太容易出错,因为它宣传并强制执行一个菜单,值的设置者可以从中选择。

回答by Simeon Visser

An Enum is useful to prevent people from doing things they shouldn't do. For example, if I have:

枚举对于防止人们做他们不应该做的事情很有用。例如,如果我有:

public final static int WEATHER_SUNNY = 0;
public final static int WEATHER_RAINY = 1;
public final static int WEATHER_HAIL = 2;

then I can do: MyClass.WEATHER_SUNNY + MyClass.WEATHER_HAILwhich makes no sense. That's why it's better to have a Weather enum. This makes the code more readable and it disallows summing two weather integers.

那么我可以这样做:MyClass.WEATHER_SUNNY + MyClass.WEATHER_HAIL这毫无意义。这就是为什么最好有一个 Weather 枚举。这使代码更具可读性,并且不允许对两个天气整数求和。

回答by JB Nizet

I would add to the other answers that enums are classes which only have a fixed number of instances. These instances may have fields and methods, and be used as any other Java objects. For example, the Monthenum could define methods like

我会补充说枚举是只有固定数量实例的类。这些实例可能具有字段和方法,并可用作任何其他 Java 对象。例如,Month枚举可以定义类似的方法

public int getNumberOfDays(int year);
public boolean isFirstOfYear();

回答by Mathieu Joets

Enum is useful when you have a list of values that are functionnally significant and finite.

当您拥有功能上重要且有限的值列表时,枚举非常有用。

Say for exemple that you need to use a day of the week : another day won't be added and you'd rather have Day.MONDAY in your code rather than a number and having to remember that 0 is monday, 1 is tuesday and so on. It makes switch statement easier to understand too, with :

例如,您需要使用一周中的某一天:不会添加另一天,您宁愿在代码中使用 Day.MONDAY 而不是数字,并且必须记住 0 是星期一,1 是星期二和很快。它也使 switch 语句更容易理解,使用:

Switch(day) { 
  case Monday : ... break;
  case Tuesday : ... break
  etc.
}

回答by Guito

Among other things, an Enum is useful to create singletons:

除其他外,Enum 可用于创建单例:

What is an efficient way to implement a singleton pattern in Java?

在 Java 中实现单例模式的有效方法是什么?

回答by Alexey A.

As goes from the name "enum" it is ideal for enumeration constants, which provide extended functionality. As for example you can store some constants comprising from several parameters like: constantId, constanNameString, additional params. Additionally enum can contain functions.

正如名称“enum”一样,它非常适用于提供扩展功能的枚举常量。例如,您可以存储一些由几个参数组成的常量,例如:constantId、constantNameString、附加参数。此外,枚举可以包含函数。

An exotic use of enum is to make a singleton.

枚举的一个奇特用途是制作单例。