开发者

Scala Enums - How to assign initial values?

object WeekDay extends Enumeration {
   type WeekDay = Value
   val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}

How would you set an initial value so WeekDay.Mon == 1 and WeekDay.Tue == 2 a开发者_开发技巧nd so on would be true?

There's a constructor in Enumeration, Enumeration(initial: Int, names: String*), is there a way I could use that to create the WeekDay object?


Try object WeekDay extends Enumeration(1) i.e. call Enumeration's constructor.

The second parameter names: String* means it accepts any number of string arguments - including none at all, hence just one argument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜