scala中枚举

scala没有从语法的角度来支持枚举,而是通过定义了一个接口Enumeration来支持的

object ExecutorState extends Enumeration{
  type ExecutorState = Value
  val LAUNCHING, LOADING, RUNNING, KILLED, FAILED, LOST, EXITED = Value
  def isFinished(state:ExecutorState):Boolean = {
    Seq(KILLED, FAILED, LOST, EXITED).contains(state)
  }
}

上面是spark中的一个例子,使用type来定义一个同名的类型, 一般就是枚举的类型.

Value的可以传递参数,有下面几种方法声明

protected final def Value : Enumeration.this.Value = { /* compiled code */ }
protected final def Value(i : scala.Int) : Enumeration.this.Value = { /* compiled code */ }
protected final def Value(name : scala.Predef.String) : Enumeration.this.Value = { /* compiled code */ }
protected final def Value(i : scala.Int, name : scala.Predef.String) : Enumeration.this.Value = { /* compiled code */ }

大致使用就这么多吧.如果有新的后面在编辑吧

posted @ 2015-08-01 22:06  高兴的博客  阅读(6750)  评论(0编辑  收藏  举报