Scala Enumerate (枚举)
来源:互联网 发布:延边电视台网络 编辑:程序博客网 时间:2024/05/16 04:51
Enumerate (枚举)
在Scala
中,没有语言级别的枚举类型,枚举的功能可以通过继承枚举类Enumeration
实现。
继承枚举类
继承枚举类Enumeration
可以在成员中使用无参方法Value
给每个枚举成员赋值。
默认的Value
方法会按变量名生成枚举名和并自动从0
开始生成枚举ID,若需要手动设定枚举名称和枚举ID则可以使用Value
方法的重载Value(id: Int, name: Strig)
。
如下所示:
object Color extends Enumeration { // 自动赋值枚举成员 val Red, Green, Blue = Value /* * 相当于分别初始化: * val Red = Value * val Green = Value * val Blue = Value */ // 手动使用 Value(id: Int, name: String) 方法手动进行id和name的设置 // 使用重载有參版本的Value(id: Int, name: String)不能一次性给多个枚举成员赋值,会编译报错(id冲突) val White = Value(100, "white") val Black = Value(200, "black")}object TestEnumeration extends App { Color.values foreach { color => println(s"ID: ${color.id}, Str: $color") }}
输出结果:
ID: 0, Str: RedID: 1, Str: GreenID: 2, Str: BlueID: 100, Str: whiteID: 200, Str: black
Enumeration
类的默认构造方法带有一个Int
类型参数,用于指定枚举ID的起始大小,如下所示:
object Color extends Enumeration(100) { val Red, Green, Blue, White, Black = Value //枚举Id将从100开始}object Main extends App { Color.values foreach { color => println(s"ID: ${color.id}, Str: $color") }}
输出结果:
ID: 100, Str: RedID: 101, Str: GreenID: 102, Str: BlueID: 103, Str: WhiteID: 104, Str: Black
访问枚举内容
枚举单例支持以多种形式访问:
- 通过枚举成员访问,类似于其它常见编程语言(
Enum.member
) - 通过枚举ID进行访问,语法类似数组(
Enum(id)
) - 通过枚举名称进行访问,使用
withName
成员方法(Enum withName "xxx"
)
枚举内部的成员全部保存在一个Set
容器中,可使用values
成员方法访问。
以前文中的Color
单例对象为例,使用多种方式访问枚举内容,如下所示:
object Color extends Enumeration { val Red, Green, Blue = Value val White = Value(100, "white") val Black = Value(200, "black")}object Main extends App { def showEnum(color: Color.Value) = println(s"ID: ${color.id}, Str: $color") // 通过枚举ID访问枚举 showEnum(Color(0)) showEnum(Color(100)) println() // 通过枚举名称访问枚举 showEnum(Color withName "Green") showEnum(Color withName "black") println() // 遍历枚举内容 Color.values foreach showEnum}
输出结果:
ID: 0, Str: RedID: 100, Str: whiteID: 1, Str: GreenID: 200, Str: blackID: 0, Str: RedID: 1, Str: GreenID: 2, Str: BlueID: 100, Str: whiteID: 200, Str: black
调用枚举类型
继承了枚举类的单例对象名并不能直接用于表示枚举类型。
对应的枚举类型应使用对象内部定义的抽象类型Value
来表示,即单例对象名称.Value
。
以前文中的Color
单例对象为例,对应的枚举类型应使用Color.Value
表示。
将枚举做为参数传递,如下所示:
object Color extends Enumeration { val Red, Green, Blue = Value val White = Value(100, "white") val Black = Value(200, "black")}object Main extends App { // Xxx.Value才是真正的枚举类型 def showEnum(color: Color.Value) = println(s"ID: ${color.id}, Str: $color") showEnum(Color.blue) showEnum(Color.white)}
输出结果:
ID: 2, Str: BlueID: 100, Str: white
限定枚举类型
Scala
通过使用单例继承Enumeration
类来提供枚举功能,枚举值的类型实际为单例.Value
。
由于Scala
中每个实例的内部类的类型均不相同,需要使用类型投影来表示通用的枚举类型。
枚举值的类型投影为Enumeration#Value
。
在泛型方法中,可以使用泛型约束T <: Enumeration#Value
来限定泛型参数为枚举类型。
如下所示:
scala> def testEnum[T <: Enumeration#Value](enum: T) = println(enum) //打印枚举参数名称testEnum: [T <: Enumeration#Value](enum: T)Unitscala> object Enum extends Enumeration { val enum0, enum1 = Value }defined object Enumscala> testEnum(Enum.enum0) //传入枚举类型参数enum0scala> testEnum(Enum.enum1)enum1scala> testEnum(123) //使用其它类型不满足泛型约束,报错<console>:13: error: inferred type arguments [Int] do not conform to method testEnum's type parameter bounds [T <: Enumeration#Value] testEnum(123) ^<console>:13: error: type mismatch; found : Int(123) required: T testEnum(123) ^
通过构建特定的枚举基类,可以实现限定指定类别的枚举。
如下所示:
scala> class MyEnumeration extends Enumeration //自定义枚举基类defined class MyEnumerationscala> def testMyEnum[T <: MyEnumeration#Value](enum: T) = println(enum) //限定枚举从自定义基类MyEnumeration中继承testMyEnum: [T <: MyEnumeration#Value](enum: T)Unitscala> object Enum extends Enumeration { val enum0, enum1 = Value } //普通枚举defined object Enumscala> object MyEnum extends MyEnumeration { val enum0, enum1 = Value } //从自定义基类MyEnumeration继承的枚举defined object MyEnumscala> testMyEnum(MyEnum.enum0) //参数为从自定义枚举基类MyEnumeration继承的枚举单例的枚举值,正常enum0scala> testMyEnum(MyEnum.enum1)enum1scala> testMyEnum(Enum.enum1) //参数为普通枚举单例的枚举值,错误<console>:14: error: inferred type arguments [Enum.Value] do not conform to method testMyEnum's type parameter bounds [T <: MyEnumeration#Value] testMyEnum(Enum.enum1) ^<console>:14: error: type mismatch; found : Enum.Value required: T testMyEnum(Enum.enum1) ^
阅读全文
0 0
- Scala Enumerate (枚举)
- python-enumerate枚举函数
- enumerate() 枚举函数
- scala枚举
- d3d11中如何枚举适配器(Enumerate Adapters)
- 2、内置函数enumerate(枚举)的用法
- 【latex】itemize, enumerate枚举,编号使用
- Python的enumerate 枚举函数如何使用?
- enumerate
- enumerate
- enumerate
- enumerate
- enumerate()
- [Scala--基础]---Scala枚举使用
- scala.Enumeration 枚举示例
- 谈谈Scala中的枚举
- Scala之枚举
- scala 枚举类
- 前端插件之侧边定位随屏幕滚动而滚动
- 设置mysql数据库为只读
- Tomcat中Servlet类的原理
- 《MySQL入门很简单》学习笔记(4)之第4章MySQL数据类型(关键词:数据库/MySQL/数据类型)
- 51nod 1002 数塔取数问题
- Scala Enumerate (枚举)
- windows下redis可视化客户端redisClient的安装及基本使用
- java发送邮件高级篇
- keras(1):DNN
- java设计模式详解----------单例模式
- mybatis学习笔记(2)
- vue在ie9中碰到的问题
- Ubuntu的遥感图像库GDAL使用
- Scala Reflect (反射)