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 */ }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM