C# Java中具有int值的枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1681976/
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
Enum with int value in Java
提问by ripper234
What's the Java equivalent of C#'s:
什么是 C# 的 Java 等价物:
enum Foo
{
Bar = 0,
Baz = 1,
Fii = 10,
}
采纳答案by Dave Webb
If you want attributes for your enum
you need to define it like this:
如果你想要你的属性,enum
你需要像这样定义它:
public enum Foo {
BAR (0),
BAZ (1),
FII (10);
private final int index;
Foo(int index) {
this.index = index;
}
public int index() {
return index;
}
}
You'd use it like this:
你会像这样使用它:
public static void main(String[] args) {
for (Foo f : Foo.values()) {
System.out.printf("%s has index %d%n", f, f.index());
}
}
The thing to realise is that enum
is just a shortcut for creating a class, so you can add whatever attributes and methods you want to the class.
需要注意的是,这enum
只是创建类的快捷方式,因此您可以向类添加任何您想要的属性和方法。
If you don't want to define any methods on your enum
you could change the scope of the member variables and make them public
, but that's not what they do in the example on the Sun website.
如果您不想定义任何方法,您enum
可以更改成员变量的范围并创建它们public
,但这不是他们在Sun 网站上的示例中所做的。
回答by Peter
Sounds like you want something like this:
听起来你想要这样的东西:
public enum Foo {
Bar(0),
Baz(1),
Fii(10);
private int number;
public Foo(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
For starters, Sun's Java Enum Tutorialwould be a great place to learn more.
对于初学者来说,Sun 的Java Enum 教程将是了解更多信息的好地方。
回答by Tendayi Mawushe
public enum Foo {
Bar(0),
Baz(1),
Fii(10);
private final int someint;
Foo(int someint) {
this.someint = someint;
}
}
In Java enums are very similar to other classes but the the Java compiler knows to treat a little differently in various situations. So if you want data in them like you seem to you need to have an instance variable for the data and an appropriate constructor.
在 Java 中,枚举与其他类非常相似,但 Java 编译器知道在各种情况下处理方式略有不同。因此,如果您希望其中的数据看起来像您一样,则需要为数据提供一个实例变量和一个适当的构造函数。
回答by Thierry-Dimitri Roy
It is:
这是:
enum Foo
{
Bar(0),
Baz(1),
Fii(10);
private int index;
private Foo(int index) {
this.index = index;
}
}
Note that to get the value of the enum from the index, Foo.valueOf(1)
(*), would not work. You need do code it yourself:
请注意,要从索引中获取枚举的值,Foo.valueOf(1)
(*) 是行不通的。您需要自己编写代码:
public Foo getFooFromIndex(int index) {
switch (index) {
case 0:
return Foo.Bar;
case 1:
return Foo.Baz;
case 10:
return Foo.Fii;
default:
throw new RuntimeException("Unknown index:" + index);
}
}
(*): Enum.valueOf() return the enum from a String. As such, you can get the value Bar with Foo.valueOf('Bar')
(*): Enum.valueOf() 从字符串返回枚举。因此,您可以获得 Bar 的值Foo.valueOf('Bar')
回答by Carl
If you have a contiguous range of values, and all you need is the integer value, you can just declare the enum minimally:
如果您有一个连续的值范围,并且您只需要整数值,则可以最低限度地声明枚举:
public enum NUMBERZ {
ZERO, ONE, TWO
}
and then obtain the int value as follows:
然后按如下方式获取 int 值:
int numberOne = NUMBERZ.ONE.ordinal();
However, if you need a discontiguous range (as in your example, where you jump from 1 to 10) then you will need to write your own enum constructor which sets your own member variable, and provide a get method for that variable, as described in the other answers here.
但是,如果您需要一个不连续的范围(如在您的示例中,您从 1 跳转到 10),那么您将需要编写自己的枚举构造函数来设置您自己的成员变量,并为该变量提供一个 get 方法,如上所述在此处的其他答案中。