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提供了一种独特的语言机制来实现这种功能: 隐式类: 允许给已有的类型添 ...
随机推荐
- STM32F103ZET6系统定时器SysTick
1.系统定时器SysTick的简介 系统定时器SysTick属于内核外设,内嵌在NVIC中.SysTick是一个24位的向下递减的计数器,计数器根据SysTick的时钟源计数,当SysTick的计数器 ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- servlet web.xml 3.1版本的头信息
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...
- String 对象-->toLowerCase() 方法
1.定义和用法 将字符串中所有的大写字符转换成小写字符,小写字符不变 返回转换后的结果字符串 语法: string.toLowerCase() 注意:不会改变字符串本身,仅以返回值的形式返回结果 举例 ...
- HAproxy 基础配置
基础配置详解 HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分 global:全局配置段 进程及安全配置相关的参数性能调整相关参数Debug参数 ...
- web网页html基础
列表标签 ul:定义一个列表 li:定义列表中的条目 图片标签 Img:用来向网页中插入一张图片 Src:指定图片所在的路径 Width:指定图片宽度 (% px) Height:指定图片高度 (% ...
- PHP代码审计(初级篇)
一.常见的PHP框架 1.zendframwork: (ZF)是Zend公司推出的一套PHP开发框架 功能非常的强大,是一个重量级的框架,ZF 用 100%面向对象编码实现. ZF 的组件结构独一无二 ...
- php.ini中文详解
[PHP] ;;;;;;;;;;;;;;;;;;;;;;; ; 关于 php.ini 配置文件 ; ;;;;;;;;;;;;;;;;;;;;;;; ; PHP 的初始化文件, 必须命名为 php.in ...
- 💕《给产品经理讲JVM》:垃圾收集器
前言 在上篇中,我们把 JVM 中的垃圾收集算法有了一个大概的了解,又是一个阴雨连绵的周末,宅在家里的我们又开始了新一轮的学习: 产品大大:上周末我们说了垃圾收集算法,下面是不是要讲一下这些算法的应用 ...
- ChaosBlade--动态脚本实现 Java 实验场景
动态脚本实现 : 参考文档:https://github.com/chaosblade-io/chaosblade/wiki/%E5%8A%A8%E6%80%81%E8%84%9A%E6%9C%AC% ...