Java 枚举被视为原始类型还是引用类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3231684/
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
Are Java enums considered primitive or reference types?
提问by zer0stimulus
If I have an enum object, is it considered a primitive or a reference?
如果我有一个枚举对象,它是原始对象还是引用?
采纳答案by SLaks
It's a reference type. Java primitives are boolean byte short char int long float double
.
它是一个引用类型。Java 原语是boolean byte short char int long float double
.
You can get the enumeration constant's value by calling ordinal()
, which is used by EnumSet and EnumMap iterator
and "traverses the elements in their natural order (the order in which the enum constants are declared)"
您可以通过调用来获取枚举常量的值ordinal()
,它由 EnumSet 和 EnumMap 使用,iterator
并“以自然顺序(声明枚举常量的顺序)遍历元素”
You can even add your own members to the enum class, like this:
您甚至可以将自己的成员添加到枚举类中,如下所示:
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } };
// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}
//Elsewhere:
Operation op = Operation.PLUS;
double two = op.eval(1, 1);
回答by TofuBeer
This articleessentially shows you how enums are implemented, and as SLaks says, they are references.
本文主要向您展示了枚举是如何实现的,正如 SLaks 所说,它们是引用。
回答by Alex VI
The way enums work is actually not too different from how they were used before their introduction with Java 5:
枚举的工作方式实际上与它们在 Java 5 引入之前的使用方式没有太大不同:
public final class Suit {
public static final Suit CLUBS = new Suit();
public static final Suit DIAMONDS = new Suit();
public static final Suit HEARTS = new Suit();
public static final Suit SPADES = new Suit();
/**
* Prevent external instantiation.
*/
private Suit() {
// No implementation
}}
By instantiating the different suits on class loading it is ensured that these will be mutually exclusive and the private constructor ensures that no further instances will be created.
通过在类加载时实例化不同的套装,可以确保它们是互斥的,并且私有构造函数确保不会创建更多实例。
These would be comparable either through == or equals.
这些将通过 == 或 equals 进行比较。
The Java 5 enum works pretty much the same way, but with some necessary features to support serialization etc.
Java 5 枚举的工作方式几乎相同,但具有一些必要的功能来支持序列化等。
I hope this background sheds some further light.
我希望这个背景能进一步阐明。
回答by K.M
Enums are reference types, in that they can have methods and can be executed from command line as well , if they have main method.
枚举是引用类型,因为它们可以有方法并且也可以从命令行执行,如果它们有 main 方法。
See following "Planet" example from Sun/Oracle
请参阅以下来自 Sun/Oracle 的“Planet”示例
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html