如何将枚举值转换为int?

发布于 2024-12-16 12:13:22 字数 364 浏览 3 评论 0原文

我有一个返回 int 类型的函数。但是,我只有 TAX 枚举的值。

如何将 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;
}

I have a function which return a type int. However, I only have a value of the TAX enumeration.

How can I cast the TAX enumeration value to an 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

憧憬巴黎街头的黎明 2024-12-23 12:13:23

您需要以某种方式使枚举公开 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();
}

(顺便说一句,我已将名称更改为更加传统和可读。)

这是假设您想要在构造函数中分配的值。如果这不是您想要的,您需要向我们提供更多信息。

You'd need to make the enum expose value somehow, e.g.

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.

嗳卜坏 2024-12-23 12:13:23

我更喜欢这个:

public enum Color {

   White,

   Green,

   Blue,

   Purple,

   Orange,

   Red
}

那么:

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

I prefer this:

public enum Color {

   White,

   Green,

   Blue,

   Purple,

   Orange,

   Red
}

then:

//cast enum to int
int color = Color.Blue.ordinal();
谈下烟灰 2024-12-23 12:13:23

有时,一些 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;

Sometime some C# approach makes the life easier in Java world..:

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;
秋风の叶未落 2024-12-23 12:13:23

如果您想要在构造函数中分配的值,则需要在枚举定义中添加一个方法来返回该值。

如果您想要一个代表枚举值的唯一数字,可以使用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().

不爱素颜 2024-12-23 12:13:23

也许使用字符串表示比使用整数更好,因为如果将值添加到枚举中,字符串仍然有效。您可以使用枚举的 name() 方法将枚举值转换为字符串,并使用枚举的 valueOf() 方法再次从字符串创建枚举表示形式。以下示例演示如何将枚举值转换为字符串并返回(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");

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. 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. The following example shows how to convert the enum value to String and back (ValueType is an enum):

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");
转身以后 2024-12-23 12:13:23
public enum ProfileType {
PRIVATE,
PUBLIC
}

private ProfileType profileType = ProfileType.PRIVATE;

如果您想从 profileType 获取 int,您可以简单地执行以下操作:

int profileTypeInt = profileType.ordinal();

public enum ProfileType {
PRIVATE,
PUBLIC
}

private ProfileType profileType = ProfileType.PRIVATE;

If you want to get int from profileType you can simply do:

int profileTypeInt = profileType.ordinal();

懵少女 2024-12-23 12:13:23
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.
}
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.
}
半衾梦 2024-12-23 12:13:23

一种稍微不同的方法(至少在 Android 上)是使用 IntDef 注释来组合一组 int 常量

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

用作函数参数:

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

或在变量声明中:

@TAX int currentTax = IMPORTEDTAX;

A somewhat different approach (at least on Android) is to use the IntDef annotation to combine a set of int constants

@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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文