简体   繁体   English

如何将枚举值转换为int?

[英]How to convert enum value to int?

I have a function which return a type int. 我有一个返回int类型的函数。 However, I only have a value of the TAX enumeration. 但是,我只有TAX枚举的值。

How can I cast the TAX enumeration value to an int? 如何将TAX枚举值转换为int?

public enum TAX {
    NOTAX(0),SALESTAX(10),IMPORTEDTAX(5);

    private int value;
    private TAX(int value){
        this.value = value;
    }
}

TAX var = TAX.NOTAX; // This value will differ

public int getTaxValue()
{
  // what do do here?
  // return (int)var;
}

You'd need to make the enum expose value somehow, eg 你需要以某种方式使枚举公开value ,例如

public enum Tax {
    NONE(0), SALES(10), IMPORT(5);

    private final int value;
    private Tax(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

...

public int getTaxValue() {
    Tax tax = Tax.NONE; // Or whatever
    return tax.getValue();
}

(I've changed the names to be a bit more conventional and readable, btw.) (我已将名称更改为更传统和可读,顺便说一下。)

This is assuming you want the value assigned in the constructor. 假设您需要在构造函数中指定的值。 If that's not what you want, you'll need to give us more information. 如果这不是您想要的,您需要向我们提供更多信息。

I prefer this: 我更喜欢这个:

public enum Color {

   White,

   Green,

   Blue,

   Purple,

   Orange,

   Red
}

then: 然后:

//cast enum to int
int color = Color.Blue.ordinal();

If you want the value you are assigning in the constructor, you need to add a method in the enum definition to return that value. 如果需要在构造函数中指定的值,则需要在枚举定义中添加一个方法以返回该值。

If you want a unique number that represent the enum value, you can use ordinal() . 如果需要表示枚举值的唯一编号,可以使用ordinal()

Sometime some C# approach makes the life easier in Java world..: 有时候一些C#方法会让Java世界的生活变得更轻松..:

class XLINK {
static final short PAYLOAD = 102, ACK = 103, PAYLOAD_AND_ACK = 104;
}
//Now is trivial to use it like a C# enum:
int rcv = XLINK.ACK;

Maybe it's better to use a String representation than an integer, because the String is still valid if values are added to the enum. 也许最好使用String表示而不是整数,因为如果将值添加到枚举中,则String仍然有效。 You can use the enum's name() method to convert the enum value to a String an the enum's valueOf() method to create an enum representation from the String again. 您可以使用enum的name()方法将枚举值转换为String和enum的valueOf()方法,以再次从String创建枚举表示。 The following example shows how to convert the enum value to String and back (ValueType is an enum): 以下示例显示如何将枚举值转换为String并返回(ValueType是枚举):

ValueType expected = ValueType.FLOAT;
String value = expected.name();

System.out.println("Name value: " + value);

ValueType actual = ValueType.valueOf(value);

if(expected.equals(actual)) System.out.println("Values are equal");
public enum Tax {

NONE(1), SALES(2), IMPORT(3);

private final int value;
    private Tax(int value) {
        this.value = value;
    }

    public String toString() {
        return Integer.toString(value);
    }
}

class Test {
    System.out.println(Tax.NONE);    //Just an example.
}

A somewhat different approach (at least on Android) is to use the IntDef annotation to combine a set of int constants 一种稍微不同的方法(至少在Android上)是使用IntDef注释来组合一组int常量

@IntDef({NOTAX, SALESTAX, IMPORTEDTAX})
@interface TAX {}
int NOTAX = 0;
int SALESTAX = 10;
int IMPORTEDTAX = 5;

Use as function parameter: 用作功能参数:

void computeTax(@TAX int taxPercentage){...} 

or in a variable declaration: 或在变量声明中:

@TAX int currentTax = IMPORTEDTAX;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM