Scala教程之:Enumeration
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的更多相关文章
- scala教程之:可见性规则
文章目录 public Protected private scoped private 和 scoped protected 和java很类似,scala也有自己的可见性规则,不同的是scala只有 ...
- Scala教程之:深入理解协变和逆变
文章目录 函数的参数和返回值 可变类型的变异 在之前的文章中我们简单的介绍过scala中的协变和逆变,我们使用+ 来表示协变类型:使用-表示逆变类型:非转化类型不需要添加标记. 假如我们定义一个cla ...
- Scala教程之:Either
在之前的文章中我们提到了Option,scala中Option表示存在0或者1个元素,如果在处理异常的时候Option就会有很大的限制,因为Option如果返回None,那么我并不知道具体的异常到底是 ...
- Scala教程之:可变和不变集合
文章目录 mutable HashMap immutable HashMap 集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力.在scala中集合主要在三个包里面:scala.coll ...
- Scala教程之:Future和Promise
文章目录 定义返回Future的方法 阻塞方式获取Future的值 非阻塞方式获取Future的值 Future链 flatmap VS map Future.sequence() VS Future ...
- Scala教程之:PartialFunction
Scala中有一个很有用的traits叫PartialFunction,我看了下别人的翻译叫做偏函数,但是我觉得部分函数更加确切. 那么PartialFunction是做什么用的呢?简单点说Parti ...
- Scala教程之:Option-Some-None
文章目录 Option和Some Option和None Option和模式匹配 在java 8中,为了避免NullPointerException,引入了Option,在Scala中也有同样的用法. ...
- Scala教程之:scala的参数
文章目录 默认参数值 命名参数 scala的参数有两大特点: 默认参数值 命名参数 默认参数值 在Scala中,可以给参数提供默认值,这样在调用的时候可以忽略这些具有默认值的参数. def log(m ...
- Scala教程之:可扩展的scala
文章目录 隐式类 限制条件 字符串插值 s 字符串插值器 f 插值器 raw 插值器 自定义插值器 Scala是扩展的,Scala提供了一种独特的语言机制来实现这种功能: 隐式类: 允许给已有的类型添 ...
随机推荐
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- (七)Spring Cloud 配置中心config
spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...
- 关于代码覆盖 or 冲突
关于代码覆盖 or 冲突 在使用git同步代码时,步骤一般为 commit -> pull -> push 那这个过程的意义何在呢? 首先是区分本地仓库 与 远程仓库,可以理解为本地git ...
- 用Fiddler抓取手机APP数据包
Fiddler下载地址 1.允许远程连接 2.允许监听https 3.重启Fiddler 这步很重要,不要忘了 4.手机配置 用ipconfig命令查询当前PC的局域网IP 将手机连接上同一个WIFI ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- Linux学习,账号管理与权限管理
linux系统本来不认识账号,只是通过UID(用户ID)和GID(所属组ID)来区分账号属性的.而这对应的目录如下: UID ===> /etc/passwd GID ===> /etc/ ...
- JAVA集合框架之List和Set、泛型
一 List是有序可重复的集合 可以进行增删改查,直接看代码 package com.collection; import java.util.ArrayList; import java.util. ...
- 23 抽象类 abstract
/*概念 * abstract:关键字,用于修饰方法和类 * 抽象方法:不同类的方法是相似,但是具体内容又不太一样,所以我们只能抽取他的声明,没有具体的方法体,没有具体方法体的方法就是抽象方法 * 抽 ...
- 7.1 java 类、(成员)变量、(成员)方法
/* * 面向对象思想: * 面向对象是基于面向过程的编程思想. * * 面向过程:强调的是每一个功能的步骤 * 面向对象:强调的是对象,然后由对象去调用功能 * * 面向对象的思想特点: * A:是 ...
- lr具体使用步骤概述
lr具体使用 1 无工具情况下的性能测试 2性能测试工具LoadRunner的工作原理 3 VuGen应用介绍 4 协议的类型及选择方法 5 脚本的创建过程 6 脚本的参数化 7 调试技术 8 Con ...