Sample class can simulate enumerated type
1, create a sealed sample class
(Not sealed can also be here just to look at sealed with keywords)
package unit6
sealed abstract class TrafficLightColor
case object Red extends TrafficLightColor
case object Yellow extends TrafficLightColor
case object Green extends TrafficLightColor
尖叫提示:这里的样例类使用object关键字而不是class关键字,是因为Scala不允许没有参数列表的样例类
2, analog enumeration
println("遍历密封样例类-------")
for (color <- Array(Red, Yellow, Green)){
println(
color match {
case Red => "红灯停"
case Yellow => "黄灯等一等"
case Green => "绿灯行"
})
}
Reproduced in: https: //www.jianshu.com/p/c78373d242e1