[Scala基础]--Scala枚举使用

参考:

http://www.scala-lang.org/api/current/scala/Enumeration.html

http://www.jianshu.com/p/675dad38c8a6

http://xiaoxin2009.github.io/Scala%E7%B1%BB%EF%BC%8C%E5%AF%B9%E8%B1%A1.html

Scala的枚举与java/c++不同,scala没有枚举类型,需要你扩展Enumeration类的对象并以Value方法调用初始化枚举中的所有可选值。

1、在使用Scala的枚举类之前,首先看看Scala源码关于枚举类的说明:

[Scala基础]--Scala枚举使用_第1张图片

翻译后的意思如下:

(1)定义特定于枚举的一组有限值。 通常,这些值列举了可能采取的所有可能的形式,并为案例类提供了轻量级的替代方案。
(2)每次调用Value方法会向枚举中添加一个新的唯一值。 为了可以访问,这些值通常被定义为评估的val成员。
(3)枚举中的所有值共享一个常见的唯一类型,定义为枚举的值类型成员(在枚举实例的稳定标识符路径上选择的值)。


2、枚举类的创建

环境:windows10+jdk-1.8+scala-2.10.5+idea-2016

(1)创建一个Object类:Margin

[Scala基础]--Scala枚举使用_第2张图片


(2)修改成为包对象类(此处表述有多种,明白就好)

[Scala基础]--Scala枚举使用_第3张图片

实现的代码如下:

/**
  * Created by yangjf on 20170928.
  * Update date:
  * Time: 14:11
  * Project: sparkmvn
  * Package: com.ngaa.bigdata.scala.core
  * Describe :  Extend the  scala.Enumeration class to create your enumeration.
  *
  * Command:
  *
  * Email:  [email protected]
  *
  * Please note:
  * Must be checked once every time you submit a configuration file is correct!
  * Data is priceless! Accidentally deleted the consequences!
  *
  */
package com.ngaa.bigdata.scala.core {

  object Margin extends Enumeration {
    type Margin = Value
    val TOP, BOTTOM, LEFT, RIGHT = Value
  }

}

3、使用枚举类

(1)创建测试类

package com.ngaa.bigdata.scala.core
import com.ngaa.bigdata.scala.core.Margin._
/**
  * Created by yangjf on 20170928.
  * Update date:
  * Time: 14:19
  * Project: sparkmvn
  * Package: com.ngaa.bigdata.scala.core
  * Describe :     
  *
  * Result of Test: test ok,test error
  * Command:
  *
  * Email:  [email protected]
  * Status:Using online
  *
  * Please note:
  * Must be checked once every time you submit a configuration file is correct!
  * Data is priceless! Accidentally deleted the consequences!
  *
  */
object TestMainEnum extends App {

  override def main(args: Array[String]) {

    // Use an enumeration value in a test.
    var currentMargin=TOP
    // Later in the code ...
    if (currentMargin == TOP) println("working on Top")
    // Print all the enumeration values
    Margin.values foreach println
    println("--------------------------------------------------")

    Margin.values filter isWorkingYop foreach println
  }

  // User-defined method.
  def isWorkingYop(d: Margin) = ! (d == TOP || d == LEFT)


   /**
     *  The output is as follows:
     * working on Top
     * TOP
     * BOTTOM
     * LEFT
     * RIGHT
     * --------------------------------------------------
     * BOTTOM
     * RIGHT
     */
}

(2)输出

/**
  *  The output is as follows:
  * working on Top
  * TOP
  * BOTTOM
  * LEFT
  * RIGHT
  * --------------------------------------------------
  * BOTTOM
  * RIGHT
  */

学习:Spark使用枚举类

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark

import org.apache.mesos.Protos.{TaskState => MesosTaskState}

private[spark] object TaskState extends Enumeration {

  val LAUNCHING, RUNNING, FINISHED, FAILED, KILLED, LOST = Value

  val FINISHED_STATES = Set(FINISHED, FAILED, KILLED, LOST)

  type TaskState = Value

  def isFailed(state: TaskState): Boolean = (LOST == state) || (FAILED == state)

  def isFinished(state: TaskState): Boolean = FINISHED_STATES.contains(state)

  def toMesos(state: TaskState): MesosTaskState = state match {
    case LAUNCHING => MesosTaskState.TASK_STARTING
    case RUNNING => MesosTaskState.TASK_RUNNING
    case FINISHED => MesosTaskState.TASK_FINISHED
    case FAILED => MesosTaskState.TASK_FAILED
    case KILLED => MesosTaskState.TASK_KILLED
    case LOST => MesosTaskState.TASK_LOST
  }

  def fromMesos(mesosState: MesosTaskState): TaskState = mesosState match {
    case MesosTaskState.TASK_STAGING => LAUNCHING
    case MesosTaskState.TASK_STARTING => LAUNCHING
    case MesosTaskState.TASK_RUNNING => RUNNING
    case MesosTaskState.TASK_FINISHED => FINISHED
    case MesosTaskState.TASK_FAILED => FAILED
    case MesosTaskState.TASK_KILLED => KILLED
    case MesosTaskState.TASK_LOST => LOST
    case MesosTaskState.TASK_ERROR => LOST
  }
}

在其他类中通过以下方式获取内容:

 TaskState.FINISHED




你可能感兴趣的:(Scala)