java java和C++之间Enum的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2080681/
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-29 19:19:11  来源:igfitidea点击:

Difference of Enum between java and C++?

javac++

提问by giri

I am learning Enums in java I like to know what are the major differences of Enum in java and C++. Thanks

我正在 Java 中学习 Enum 我想知道 Enum 在 Java 和 C++ 中的主要区别是什么。谢谢

回答by josefx

In c++ an enum is just a list of integer values. In java an enum is a class which extends Enum and is more a nice way to write:

在 C++ 中,枚举只是一个整数值列表。在 java 中,枚举是一个扩展 Enum 的类,它更像是一种很好的编写方式:

class MyEnum extends Enum<MyEnum>
{
public final static MyEnum VE01 = new MyEnum();
public final static MyEnum VE02 = new MyEnum();
}

as enum:

作为枚举:

enum MyEnum
{
 VE01,VE02;
}

For the Methods of enum see this. Since a java enum is an object it supports everything a normal java object does. as giving them values or functions:

对于枚举的方法,请参阅this。由于 java 枚举是一个对象,它支持普通 java 对象所做的一切。作为给他们价值或功能:

enum Binary{
 ZERO(0),ONE(1);
 Binary(int i)
 {
   value = i;
 }
 public final int value;
}

a nice one is anonymous classes:

一个不错的方法是匿名类:

enum State{
StateA{
   public State doSomething()
   {
     //state code here
     return StateB;
    }
},
StateB{
    public State doSomething()
    {
       return StateA;
    }
};
public abstract State doSomething();
}

回答by Kent Boogaart

In C++, an enumeration is just a set of named, integral constants. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration.

在 C++ 中,枚举只是一组命名的整数常量。在 Java 中,枚举更像是类的命名实例。您可以自定义枚举中可用的成员。

Also, C++ will implicitly convert enum values to their integral equivalent, whereas the conversion must be explicit in Java.

此外,C++ 将隐式地将枚举值转换为它们的等效整数,而在 Java 中转换必须是显式的。

More information available on Wikipedia.

维基百科上提供了更多信息。

回答by Itay Maman

Enums in C/C++ are plain Integers.

C/C++ 中的枚举是纯整数。

Enums in Java are objects - they can have methods (with different behavior from one enum instance to the other). Moreoever, the enum class supplies methods which allows iteration over all enum instances and lookup of an enum instance.

Java 中的枚举是对象——它们可以有方法(从一个枚举实例到另一个具有不同的行为)。此外,枚举类提供了允许迭代所有枚举实例和查找枚举实例的方法。

C++:

C++:

typedef enum { Red, Yellow, Green } Colors;

void canCrossIntersection(Colors c) {
   return c == Green;
}

Java:

爪哇:

public enum Colors {
  RED(false),
  Yellow(false),
  Green(true) 
  ;

  Color(boolean b) { this.b = b; }
  private boolean b;

  public boolean canCrossIntersection() { return b; }
}

回答by helpermethod

In Java you can even emulated extensible enums by letting them implement the same interface and then add all of their values to some sort of collection by calling their values() method.

在 Java 中,您甚至可以通过让它们实现相同的接口来模拟可扩展枚举,然后通过调用它们的 values() 方法将它们的所有值添加到某种集合中。

回答by Dragontamer5788

For a semi-answer... C++0x Strongly Typed Enums bring many of the benefits of Java Enums to C++: strong typing, scope and so forth. If your compiler supports C++0x Strongly Typed Enums, you should consider using them.

对于半答案... C++0x 强类型枚举为 C++ 带来了 Java 枚举的许多好处:强类型、作用域等等。如果您的编译器支持 C++0x 强类型枚举,您应该考虑使用它们。

Reference: http://www2.research.att.com/~bs/C++0xFAQ.html#enum

参考:http: //www2.research.att.com/~bs/C++0xFAQ.html#enum

回答by Amir Moghimi

A great feature of Java Enums which lacks in C++ is the name()method. This way you can get the Enum value name (as written in the enum definition) without any extra line of code in definition. For example:

C++ 中缺少的 Java Enums 的一个重要特性是name()方法。通过这种方式,您可以获得枚举值名称(如枚举定义中所写),而无需在定义中添加任何额外的代码行。例如:

enum Type {T1, T2, T3}
Type t = Type.T1;   
System.out.println(t.name()); // prints T1