简体   繁体   English

Scala枚举可以映射到Java之类的Integer吗?

[英]Can Scala enumeration maps to an Integer like Java?

I have an original Java enumeration class, which I can get the corresponding integer value using: 我有一个原始的Java枚举类,可以使用以下方法获取相应的整数值:

Integer blackInteger = ColorEnum.BLACK.getValue()

public enum ColorEnum {

    BLACK(0), BLUE(1), RED(2);

    private int value;

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

    public int getValue() {
        return value;
    }
}

I then tried to write the enum class in Scala: 然后,我尝试在Scala中编写枚举类:

object ColorEnum extends Enumeration {
  val BLACK, BLUE, RED = Value
}

I am wondering how do I get the integer value of ColorEnum.BLACK in Scala? 我想知道如何在Scala中获取ColorEnum.BLACK的整数值吗?

Thanks a lot! 非常感谢!

Use id like so: 像这样使用id

ColorEnum.BLACK.id

Enumeration.Value in the Scaladoc. Enumeration.Value在Scaladoc。

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

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