简体   繁体   中英

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:

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:

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

I am wondering how do I get the integer value of ColorEnum.BLACK in Scala?

Thanks a lot!

Use id like so:

ColorEnum.BLACK.id

Enumeration.Value in the Scaladoc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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