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的博客

最新文章

  1. Linux:history命令记录操作时间、操作用户、操作IP
  2. SQL Server 中的逻辑读与物理读
  3. spl_autoload_register装在函数的正确写法
  4. swift 如何删除subviews
  5. java常见异常集锦
  6. [C和指针]第二部分
  7. android 案例:从另一个activity选择信息并获取返回值
  8. Eclipse Key Shortcuts for Greater Developers Productivity--reference
  9. HDU1465 第六周L题(错排组合数)
  10. H.264 Quantization
  11. How To Learn English Very Fast
  12. 解决linux ping: unknown host www.baidu.com(转)
  13. Markdown 练习
  14. mySQL、mariaDB、noSQL、SQL server、redis之间是什么关系?
  15. 跨域的另一种解决方案CORS(CrossOrigin Resource Sharing)跨域资源共享
  16. python3 shell 中添加清屏
  17. wget命令的几个常用选项和示例
  18. [AHOI2014/JSOI2014] 解题报告
  19. Java SDK夯住(Hang)问题排查
  20. C#.NET常见问题(FAQ)-索引器indexer有什么用

热门文章

  1. storm学习初步
  2. D 楼房重建
  3. NKOJ 7.7练习题A IP地址
  4. Aplayer搭配Metingjs音乐插件的使用
  5. idea运行javadoc生成文档以及 报错编码gbk的不可映射字符坑
  6. Light of future-冲刺Day 6
  7. GlusterFS 4.1 版本选择和部署
  8. Docker多网卡
  9. 多级分销概念 MongoDB||MySQL
  10. Linux bash篇(三 数据流重定向)