【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
Scala枚举的简单使用
Scala只提供了Enumeration抽象类来定义枚举,如下是一个枚举的定义。
/**
* Created by xinxingegeya on 16/1/15.
*/
object Breed extends Enumeration {
// 抽象类型
type Breed = Value
val doberman = Value("Doberman Pinscher")
val yorkie = Value("Yorkshire Terrier")
val scottie = Value("Great Dane")
val portie = Value("Portuguese Water Dog")
}
如下定义一周的枚举,
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
对枚举的操作和遍历,
object Breed extends Enumeration {
// 抽象类型
type Breed = Value
val doberman = Value("Doberman Pinscher")
val yorkie = Value("Yorkshire Terrier")
val scottie = Value("Great Dane")
val portie = Value("Portuguese Water Dog")
}
println(Breed.doberman)
import Breed._
println(doberman)
println("ID\tBreed")
for (breed <- Breed.values) println(s"${breed.id}\t$breed")
println("\njust Terriers:")
Breed.values filter (_.toString.endsWith("Terrier")) foreach println
=========END=========
来源:oschina
链接:https://my.oschina.net/u/1469576/blog/603338