枚举在Java中使用int值。

时间:2023-01-26 15:34:48

What's the Java equivalent of C#'s:

Java的c#是什么?

enum Foo
{
  Bar = 0,
  Baz = 1,
  Fii = 10,
}

5 个解决方案

#1


80  

If you want attributes for your enum you need to define it like this:

如果您想要枚举的属性,您需要这样定义它:

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上定义任何方法,您可以更改成员变量的范围并将它们公开,但Sun网站上的示例中并没有这样做。

#2


15  

If you have a contiguous range of values, and all you need is the integer value, you can just declare the enum minimally:

如果您有一个连续的值范围,并且您所需要的是整数值,那么您只需以最小的方式声明enum:

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),那么您将需要编写自己的enum构造函数,该构造函数设置自己的成员变量,并为该变量提供get方法,如这里的其他答案所述。

#3


4  

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)(*)获取enum的值,将不能工作。你需要自己编写代码:

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')

(*):枚举。valueof()从字符串返回enum。因此,可以使用Foo.valueOf('Bar')获取值条

#4


2  

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 Tutorial would be a great place to learn more.

首先,Sun的Java Enum教程将是学习更多内容的好地方。

#5


0  

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 enums中与其他类非常相似,但是Java编译器知道在不同的情况下使用不同的方法。所以如果你想要数据像你看起来的那样,你需要一个数据实例变量和一个合适的构造函数。

#1


80  

If you want attributes for your enum you need to define it like this:

如果您想要枚举的属性,您需要这样定义它:

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上定义任何方法,您可以更改成员变量的范围并将它们公开,但Sun网站上的示例中并没有这样做。

#2


15  

If you have a contiguous range of values, and all you need is the integer value, you can just declare the enum minimally:

如果您有一个连续的值范围,并且您所需要的是整数值,那么您只需以最小的方式声明enum:

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),那么您将需要编写自己的enum构造函数,该构造函数设置自己的成员变量,并为该变量提供get方法,如这里的其他答案所述。

#3


4  

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)(*)获取enum的值,将不能工作。你需要自己编写代码:

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')

(*):枚举。valueof()从字符串返回enum。因此,可以使用Foo.valueOf('Bar')获取值条

#4


2  

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 Tutorial would be a great place to learn more.

首先,Sun的Java Enum教程将是学习更多内容的好地方。

#5


0  

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 enums中与其他类非常相似,但是Java编译器知道在不同的情况下使用不同的方法。所以如果你想要数据像你看起来的那样,你需要一个数据实例变量和一个合适的构造函数。