黑猴子的家:Scala 枚举

Scala中没有枚举类型,定义一个扩展Enumeration类的对象,并以value调用初始化枚举中的所有可能值:

1、extends Enumeration

object TrafficLightColor extends Enumeration {
  val Red = Value(0, "Stop")
  val Yellow = Value(1, "Slow")
  val Green = Value(2, "Go")
}

2、TrafficLightColorMain

object TrafficLightColorMain {
  def main(args: Array[String]): Unit = {
    println(TrafficLightColor.Red)
    println(TrafficLightColor.Red.id) 
  }
}

转载于:https://www.jianshu.com/p/9c15294bb447

猜你喜欢

转载自blog.csdn.net/weixin_34392906/article/details/91182483