如何通过反射获取对象中的所有对象?
考虑以下代码:
object MonthDay extends MyEnum {
//Some important holidays
object NewYear extends MonthDay( 1, 1)
object UnityDay extends MonthDay(11, 9)
object SaintNicholas extends MonthDay(12, 6)
object Christmas extends MonthDay(12, 24)
}
class MonthDay(month: Int, day: Int)
trait MyEnum {
val values: List[MonthDay] = this.getClass.getField("MODULE$")...
val next: MonthDay = ...
val previous: MonthDay = ...
}
//Of course the user can create his own MonthDays
val myBirthDay = new MonthDay(month, day)
if(!MonthDay.values.contains(myBirthDay)) "Well, I probably have to work"
else "Great, it is a holiday!"
我希望有一个特性(MyEnum),我可以将其混合到持有我的“枚举对象”的对象中,并使用方法返回它们的列表(def值:List [MonthDay])或迭代它们(def next:MonthDay或def previous:MonthDay)没有重复几次(这绝对是至关重要的!).
我们的想法是,值访问MonthDay对象,并通过反射查找它们正在扩展的类(MonthDay)的所有单例对象.
最佳答案 我的解决方案基于
Landei’s answer将是:
trait MyEnum{
def valsOfType[T:Manifest] = {
val c=implicitly[Manifest[T]].erasure
for {m <- getClass.getMethods
if m.getParameterTypes.isEmpty && c.isAssignableFrom(m.getReturnType)
} yield (m.invoke(this).asInstanceOf[T])
}
}
class MonthDay(month:Int,day:Int)
object MonthDay extends MyEnum {
//maybe you want to call this "holidays" instead
lazy val values = valsOfType[MonthDay]
val NewYear = new MonthDay( 1, 1)
val UnityDay = new MonthDay(11, 9)
val SaintNicholas = new MonthDay(12, 6)
val Christmas = new MonthDay(12, 24)
}
我不认为你应该再调用这个MyEnum,因为枚举类型意味着一组封闭的值.
(如果枚举值定义为对象,则不起作用)