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