首页 > 其他 > 详细

scala中枚举

时间:2015-08-01 23:22:56      阅读:270      评论:0      收藏:0      [点我收藏+]

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

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

scala中枚举

原文:http://www.cnblogs.com/gaoxing/p/4694753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!