Scala教程之:Enumeration

作者: flydean程序那些事 | 来源:发表于2019-12-16 22:40 被阅读0次

Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enumeration的定义:

abstract class Enumeration (initial: Int) extends Serializable 

Enumeration是一个抽象类,它定义四个value方法,来设置内部的值, 四个value方法如下定义:

  /** Creates a fresh value, part of this enumeration. */
  protected final def Value: Value = Value(nextId)

  /** Creates a fresh value, part of this enumeration, identified by the
   *  integer `i`.
   *
   *  @param i An integer that identifies this value at run-time. It must be
   *           unique amongst all values of the enumeration.
   *  @return  Fresh value identified by `i`.
   */
  protected final def Value(i: Int): Value = Value(i, nextNameOrNull)

  /** Creates a fresh value, part of this enumeration, called `name`.
   *
   *  @param name A human-readable name for that value.
   *  @return  Fresh value called `name`.
   */
  protected final def Value(name: String): Value = Value(nextId, name)

  /** Creates a fresh value, part of this enumeration, called `name`
   *  and identified by the integer `i`.
   *
   * @param i    An integer that identifies this value at run-time. It must be
   *             unique amongst all values of the enumeration.
   * @param name A human-readable name for that value.
   * @return     Fresh value with the provided identifier `i` and name `name`.
   */
  protected final def Value(i: Int, name: String): Value = new Val(i, name)

知道如何设置Enum的值后,我们就可以尝试创建一个Enum了。

println("Step 1: How to create an enumeration")
object Donut extends Enumeration {
type Donut = Value

val Glazed      = Value("Glazed")
val Strawberry  = Value("Strawberry")
val Plain       = Value("Plain")
val Vanilla     = Value("Vanilla")
}

上面的例子中,我们创建了一个Enum,并且设置了几个值。

下面我们看下怎么取到Enum的值:

println("\nStep 2: How to print the String value of the enumeration")
println(s"Vanilla Donut string value = ${Donut.Vanilla}")

你可以看到如下的输出:


Step 2: How to print the String value of the enumeration
Vanilla Donut string value = Vanilla

下面是怎么输出Enum的id:

println("\nStep 3: How to print the id of the enumeration")
println(s"Vanilla Donut's id = ${Donut.Vanilla.id}")

结果如下:

Step 3: How to print the id of the enumeration
Vanilla Donut's id = 3

怎么输出所有的Enum项呢?

println("\nStep 4: How to print all the values listed in Enumeration")
println(s"Donut types = ${Donut.values}")

输出结果如下:

Step 4: How to print all the values listed in Enumeration
Donut types = Donut.ValueSet(Glazed, Strawberry, Plain, Vanilla)

接下来,我们看下怎么打印出所有的Enum:

println("\nStep 5: How to pattern match on enumeration values")
Donut.values.foreach {
  case d if (d == Donut.Strawberry || d == Donut.Glazed) => println(s"Found favourite donut = $d")
  case _ => None
}

输出如下:


Step 5: How to pattern match on enumeration values
Found favourite donut = Glazed
Found favourite donut = Strawberry

最后,我们看下怎么改变Enum值的顺序:

println("\nStep 6: How to change the default ordering of enumeration values")
object DonutTaste extends Enumeration{
  type DonutTaste = Value

  val Tasty       = Value(0, "Tasty")
  val VeryTasty   = Value(1, "Very Tasty")
  val Ok          = Value(-1, "Ok")
}

println(s"Donut taste values = ${DonutTaste.values}")
println(s"Donut taste of OK id = ${DonutTaste.Ok.id}")

输出结果如下:


Step 6: How to change the default ordering of enumeration values
Donut taste values = DonutTaste.ValueSet(Ok, Tasty, Very Tasty)
Donut taste of OK id = -1

更多教程请参考 flydean的博客

相关文章

  • Scala教程之:Enumeration

    Enumeration应该算是程序语言里面比较通用的一个类型,在scala中也存在这样的类型, 我们看下Enume...

  • Scala Enumeration

    Value是Enumeration中的一个内部类,同名的Value方法能够返回类的一个实例,在这里的意思就是说值...

  • Scala中的枚举Enumeration

    总览 本文将会对scala中的枚举类型实现进行深入的探讨,并且和其他语言中的枚举做简单的比较。 什么是枚举 现实中...

  • scala 枚举

    scala没有枚举类型, 标准库定义了一个Enumeration的助手类, 可以用于产出枚举 示例代码: 每次调用...

  • Scala中的枚举

    与java不同,scala并没有提供枚举类型,而是通过Enumeration类的帮助来实现枚举功能的。 使用方法 ...

  • spark shell

    1. 启动shell 2. 载入需要分析的数据 编程之scala版 编程之python版

  • Scala教程之:Scala基础

    这篇文章我们大概过一下Scala的基础概念,后面的文章我们会有更详细的讲解Scala的具体内容。 常量 在Scal...

  • Enumerations

    Enumeration Syntax The name of enumeration starts with a ...

  • Scala教程之:scala的参数

    scala的参数有两大特点: 默认参数值 命名参数 默认参数值 在Scala中,可以给参数提供默认值,这样在调用的...

  • Enumeration与Iterator介绍

    Enumeration Enumeration简介 Enumeration(列举),本身是一个接口,不是一个类。E...

网友评论

    本文标题:Scala教程之:Enumeration

    本文链接:https://www.haomeiwen.com/subject/jxjjnctx.html