Scala 枚举 - 如何分配初始值?
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
您将如何设置初始值以使 WeekDay.Mon == 1
和 WeekDay.Tue == 2
等为真?
枚举中有一个构造函数,Enumeration(initial: Int, names: String*)
,有没有办法使用它来创建 WeekDay 对象?
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
and 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
object WeekDay extends Enumeration(1)
即调用 Enumeration 的构造函数。第二个参数
names: String*
表示它接受任意数量的字符串参数 - 包括根本没有字符串参数,因此只有一个参数。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.