如何获得枚举值的枚举定义? [英] How to get Enumeration Value's Enumeration definition?
问题描述
object TestEnum extends Enumeration{
val One = Value("One")
val Two,Three= Value
}
println(TestEnum.One.getClass)
println(TestEnum.One.getClass.getDeclaringClass)//get Enumeration
所以我的问题是如何从TestEnum.One获取Class [TestEnum]?
So my question is how to get Class[TestEnum] from TestEnum.One?
谢谢。
推荐答案
我认为您不能不幸。 TestEnum.One
实际上只是类 Enumeration#Value
的一个实例。实际上,不仅如此,它还差很多-您的枚举值都按类型擦除到同一件事:
I don't think you can unfortunately. TestEnum.One
is really just an instance of the class Enumeration#Value
. In fact, it's lots worse than just this - your enumeration values are all erased by type to the same thing:
object Weekday extends Enumeration {
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
def foo(w: Weekday.Value)
def foo(e: TestEnum.Value) //won't compile as they erase to same type
枚举的实例只是 Enumeration#Value
的实例,它们的声明类仅为 scala.Enumeration
。
As the instances of your enumeration are just instances of Enumeration#Value
, their declaring class is just scala.Enumeration
.
这令人沮丧,但似乎这些scala枚举比没有用的更糟;如果您通过序列化传递它们(至少在2.7.7中),那么您也不能进行相等性检查!
It's frustrating but it seems like these scala enums are worse than useless; if you pass them through serialization (at least in 2.7.7), then you can't do equality checks either!
这篇关于如何获得枚举值的枚举定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!