在Java中Enum和基于枚举的类实现之间的优缺点是什么?

时间:2023-01-26 15:11:35

I've recently come across an article discussing the use of an enum-based class implementation in C#, which is quite impressive. The second one here is in Java. However, my colleagues suggest me to use Enum instead.

我最近遇到过一篇文章,讨论在C#中使用基于枚举的类实现,这非常令人印象深刻。这里的第二个是Java。但是,我的同事建议我改用Enum。

Could anyone point out any pros and cons using each of them, and when to use it?

任何人都可以指出使用它们的任何利弊,何时使用它?

5 个解决方案

#1


1  

In Java, Enum types act as a class that is declared with their unique name. It is pretty much like the any other class that is designed to create constant values. Recently, I also came across to an info that before the declaration of Enums in Java, an enum like class was created. Just like the article that was suggested on this question, it seems that previous to JVM 1.5, class based enums were widely used.

在Java中,Enum类型充当以其唯一名称声明的类。它与设计用于创建常量值的任何其他类非常相似。最近,我还发现了一个信息,在Java中声明Enums之前,创建了一个类似枚举的类。就像在这个问题上提出的文章一样,似乎JVM 1.5之前的基于类的枚举被广泛使用。

You can check this source: http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

您可以查看以下来源:http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

I think it is a very good explanation on Java Enums and Why they are created. The article claims 3 advantages for Enum:

我认为这是对Java Enums及其创建原因的一个非常好的解释。该文章声称Enum有3个优点:

1)Type Safety.

2)Unless the class was worked thoroughly, the Enum class was prone to printing problems. When coder wanted a string result to be returned, an primitive value was returned. To my experience, with some additions to the class, this is avoided. But question is, is it convenient for the coder.

2)除非课程工作彻底,否则Enum课程容易出现打印问题。当编码器想要返回字符串结果时,返回原始值。根据我的经验,通过对课程的一些补充,可以避免这种情况。但问题是,编码器是否方便。

3)Again, access was based on an instance of the class. Thus, coder cannot access to the Enum option directly. Coder must use the class name.

3)同样,访问基于类的实例。因此,编码器无法直接访问Enum选项。编码器必须使用类名。

As a result: for convenience and code readability issues, Enums are a good choice. Plus, Enum Structure is similar to an individual classes that are nested within a carrier class. If coder wants to enhance the Enum Design and create their own style, they can turn back to the old manually coded class based system.

因此:为了方便和代码可读性问题,Enums是一个不错的选择。另外,Enum Structure类似于嵌套在运营商类中的单个类。如果编码器想要增强Enum Design并创建他们自己的风格,他们可以回到旧的手动编码的类系统。

#2


4  

The Java article you quote is from 2001. Back then, Java didn't have enums, and the methods the author describes are what programmers used to do back then to work around Java's deficiency. Java 5 introduced enums in 2004 and now the older patterns are obsolete. So your colleagues are rght: you should use enums.

您引用的Java文章来自2001年。那时,Java没有枚举,作者描述的方法是程序员当时用来解决Java缺陷的方法。 Java 5在2004年引入了枚举,现在旧的模式已经过时了。所以你的同事是对的:你应该使用枚举。

#3


2  

The Java standard enum implementation is already fully class based - you can define any methods, member variables, etc you like inside standard Java enums.

Java标准枚举实现已经完全基于类 - 您可以在标准Java枚举中定义您喜欢的任何方法,成员变量等。

There is an excellent description of this with examples in the official enum documentation:

官方枚举文档中的示例有一个很好的描述:

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Additionally the EnumSet, EnumMap, etc collection classes are extremely powerful and efficient. EnumSet has similar performance to using raw bitfields! You only get access to those classes if you use a proper enum though.

此外,EnumSet,EnumMap等集合类非常强大和高效。 EnumSet与使用原始位域具有相似的性能!如果您使用正确的枚举,则只能访问这些类。

#4


1  

The major difference is that Java's enums are more simple, one may not switch on the C# enum-based class implementation and enum-based class is more of a class than of an enumerated data type, i.e. it can be extended. Whereas enum can't be derived from another class and can not be extended.

主要区别在于Java的枚举更简单,可能无法切换基于C#enum的类实现,而基于枚举的类更多是类,而不是枚举数据类型,即可以扩展。枚举不能从另一个类派生而且不能扩展。

Java alternative for C# enum-based class could be like:

基于C#enum的类的Java替代方案可能如下:

public abstract static class CreditCard {
    enum CreditCardType{
        AMERICAN_EXPRESS, MASTER, VISA, DISCOVER;
    }

    CreditCardType type;

    public abstract void operation1();
    public abstract void operation2();
}

#5


0  

HI I will suggest to use enum if you know how to use it. because their are many reasons some of them are

如果您知道如何使用它,我建议使用枚举。因为他们中的一些原因很多

  1. uses less memory
  2. 使用更少的内存

  3. having some constant value
  4. 有一些恒定的价值

  5. less process time
  6. 减少处理时间

  7. easy to understand
  8. 容易明白

  9. reuseability
  10. easy to debug
  11. 易于调试

like that it is having many advantage but other-hand it is having many disadvantage also like

就像它具有许多优点,但另一方面它也有许多缺点

  1. limited use means we are having some limitation by using enum
  2. 限制使用意味着我们使用枚举有一些限制

#1


1  

In Java, Enum types act as a class that is declared with their unique name. It is pretty much like the any other class that is designed to create constant values. Recently, I also came across to an info that before the declaration of Enums in Java, an enum like class was created. Just like the article that was suggested on this question, it seems that previous to JVM 1.5, class based enums were widely used.

在Java中,Enum类型充当以其唯一名称声明的类。它与设计用于创建常量值的任何其他类非常相似。最近,我还发现了一个信息,在Java中声明Enums之前,创建了一个类似枚举的类。就像在这个问题上提出的文章一样,似乎JVM 1.5之前的基于类的枚举被广泛使用。

You can check this source: http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

您可以查看以下来源:http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

I think it is a very good explanation on Java Enums and Why they are created. The article claims 3 advantages for Enum:

我认为这是对Java Enums及其创建原因的一个非常好的解释。该文章声称Enum有3个优点:

1)Type Safety.

2)Unless the class was worked thoroughly, the Enum class was prone to printing problems. When coder wanted a string result to be returned, an primitive value was returned. To my experience, with some additions to the class, this is avoided. But question is, is it convenient for the coder.

2)除非课程工作彻底,否则Enum课程容易出现打印问题。当编码器想要返回字符串结果时,返回原始值。根据我的经验,通过对课程的一些补充,可以避免这种情况。但问题是,编码器是否方便。

3)Again, access was based on an instance of the class. Thus, coder cannot access to the Enum option directly. Coder must use the class name.

3)同样,访问基于类的实例。因此,编码器无法直接访问Enum选项。编码器必须使用类名。

As a result: for convenience and code readability issues, Enums are a good choice. Plus, Enum Structure is similar to an individual classes that are nested within a carrier class. If coder wants to enhance the Enum Design and create their own style, they can turn back to the old manually coded class based system.

因此:为了方便和代码可读性问题,Enums是一个不错的选择。另外,Enum Structure类似于嵌套在运营商类中的单个类。如果编码器想要增强Enum Design并创建他们自己的风格,他们可以回到旧的手动编码的类系统。

#2


4  

The Java article you quote is from 2001. Back then, Java didn't have enums, and the methods the author describes are what programmers used to do back then to work around Java's deficiency. Java 5 introduced enums in 2004 and now the older patterns are obsolete. So your colleagues are rght: you should use enums.

您引用的Java文章来自2001年。那时,Java没有枚举,作者描述的方法是程序员当时用来解决Java缺陷的方法。 Java 5在2004年引入了枚举,现在旧的模式已经过时了。所以你的同事是对的:你应该使用枚举。

#3


2  

The Java standard enum implementation is already fully class based - you can define any methods, member variables, etc you like inside standard Java enums.

Java标准枚举实现已经完全基于类 - 您可以在标准Java枚举中定义您喜欢的任何方法,成员变量等。

There is an excellent description of this with examples in the official enum documentation:

官方枚举文档中的示例有一个很好的描述:

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Additionally the EnumSet, EnumMap, etc collection classes are extremely powerful and efficient. EnumSet has similar performance to using raw bitfields! You only get access to those classes if you use a proper enum though.

此外,EnumSet,EnumMap等集合类非常强大和高效。 EnumSet与使用原始位域具有相似的性能!如果您使用正确的枚举,则只能访问这些类。

#4


1  

The major difference is that Java's enums are more simple, one may not switch on the C# enum-based class implementation and enum-based class is more of a class than of an enumerated data type, i.e. it can be extended. Whereas enum can't be derived from another class and can not be extended.

主要区别在于Java的枚举更简单,可能无法切换基于C#enum的类实现,而基于枚举的类更多是类,而不是枚举数据类型,即可以扩展。枚举不能从另一个类派生而且不能扩展。

Java alternative for C# enum-based class could be like:

基于C#enum的类的Java替代方案可能如下:

public abstract static class CreditCard {
    enum CreditCardType{
        AMERICAN_EXPRESS, MASTER, VISA, DISCOVER;
    }

    CreditCardType type;

    public abstract void operation1();
    public abstract void operation2();
}

#5


0  

HI I will suggest to use enum if you know how to use it. because their are many reasons some of them are

如果您知道如何使用它,我建议使用枚举。因为他们中的一些原因很多

  1. uses less memory
  2. 使用更少的内存

  3. having some constant value
  4. 有一些恒定的价值

  5. less process time
  6. 减少处理时间

  7. easy to understand
  8. 容易明白

  9. reuseability
  10. easy to debug
  11. 易于调试

like that it is having many advantage but other-hand it is having many disadvantage also like

就像它具有许多优点,但另一方面它也有许多缺点

  1. limited use means we are having some limitation by using enum
  2. 限制使用意味着我们使用枚举有一些限制