Dowemo

enumerate ( enumeration )

In Scala, there's no enumeration type, the function of the enumeration can inherit the enumeration class Enumeration implementation by.

Inheritance enumeration class

Inherited enumeration class Enumeration can assign a value to each enumeration member in a member by using a method Value in a member.
A default Value method is generated by 0 id and automatically generated from to, if you need to manually set the enumeration name and enumeration id to use the method overload Value(id: Int, name: Strig) .

As follows:

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")


 }



}


Output results:

ID: 0, Str: Red


ID: 1, Str: Green


ID: 2, Str: Blue


ID: 100, Str: white


ID: 200, Str: black


A default co tructor for Enumeration classes takes a Int type parameter to specify the start size of the that enumerates id, as follows:

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")


 }



}


Output results:

ID: 100, Str: Red


ID: 101, Str: Green


ID: 102, Str: Blue


ID: 103, Str: White


ID: 104, Str: Black


Access enumeration content

Enumeration singleton support in multiple forms:

  • Accessing member access, similar to other common programming languages ( Enum.member )
  • By enumerating id, the syntax is similar to arrays ( Enum(id) )
  • Access through the enumeration name, using the withName member method ( Enum withName"xxx" )

Membe & within an enumeration are all saved in a Set container that can be accessed using the values member method.

An example of a Color singleton object in a previous article is to access enumeration content in a variety of ways, as follows:

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


}


Output results:

ID: 0, Str: Red


ID: 100, Str: white



ID: 1, Str: Green


ID: 200, Str: black



ID: 0, Str: Red


ID: 1, Str: Green


ID: 2, Str: Blue


ID: 100, Str: white


ID: 200, Str: black


Calling enumeration types

The singleton object name of the enumerated class is inherited and the cannot be used directly to represent enum types.
Corresponding enumeration types should be represented by an abstract type Value defined inside the object, 单例对象名称.Value.

An example of a Color singleton object in a previous article, the corresponding enumeration type should use Color.Value representation.
Pass the enumeration as a parameter, as follows:

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)


}


Output results:

ID: 2, Str: Blue


ID: 100, Str: white


Qualify enumeration types

By using a singleton inheritance class, Scala provides an enumeration of the values that are actually 单例.Value with the enumeration.
Because the type of each I & tance of the Scala isn't the same as that of each I & tance in, you need to use the project to represent the common enumeration type.
A type of enumeration value is projected into.
In a generic method, you can use a generic co & TRAI not T <: Enumeration#Value to qualify generic paramete & as enumeration types.
As follows:

scala> def testEnum[T <: Enumeration#Value](enum: T) = println(enum) //打印枚举参数名称


testEnum: [T <: Enumeration#Value](enum: T)Unit



scala> object Enum extends Enumeration { val enum0, enum1 = Value }


defined object Enum



scala> testEnum(Enum.enum0) //传入枚举类型参数


enum0



scala> testEnum(Enum.enum1)


enum1



scala> 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)


 ^


By building a specific enumeration base class, you can implement an enumeration of for a specified class of .
As follows:

scala> class MyEnumeration extends Enumeration//自定义枚举基类


defined class MyEnumeration



scala> def testMyEnum[T <: MyEnumeration#Value](enum: T) = println(enum) //限定枚举从自定义基类MyEnumeration中继承


testMyEnum: [T <: MyEnumeration#Value](enum: T)Unit



scala> object Enum extends Enumeration { val enum0, enum1 = Value } //普通枚举


defined object Enum



scala> object MyEnum extends MyEnumeration { val enum0, enum1 = Value } //从自定义基类MyEnumeration继承的枚举


defined object MyEnum



scala> testMyEnum(MyEnum.enum0) //参数为从自定义枚举基类MyEnumeration继承的枚举单例的枚举值,正常


enum0



scala> testMyEnum(MyEnum.enum1)


enum1



scala> 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)


 ^





Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs