如何通过在反射中混合特征来访问对象内的对象? [英] How to access objects within an object by mixing in a trait with reflection?

查看:98
本文介绍了如何通过在反射中混合特征来访问对象内的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请考虑以下代码:

 对象MonthDay扩展MyEnum {
//某些重要的假期
对象NewYear extends MonthDay(1,1)
object UnityDay extends MonthDay(11,9)
对象圣尼古拉斯延伸MonthDay(12,6)
对象圣诞节延长MonthDay(12,24)
}

class MonthDay(month:Int,day:Int)

trait MyEnum {
val值:List [MonthDay] = this.getClass.getField(MODULE $)...
val下一个:MonthDay = ...
val上一个:MonthDay = ...
}

//当然用户可以创建自己的MonthDays
val myBirthDay = new MonthDay(month,day)

如果(!MonthDay.values.contains(myBirthDay))嗯,我可能要工作
其他太好了,这是一个假期!

我想拥有一个特质( MyEnum )我可以混合到持有我的枚举对象的对象的方法返回一个列表( def values:List [MonthDay] )或迭代他们( def next:MonthDay def previous:MonthDay 不重复自己几次绝对关键!)。



想法是访问 MonthDay object,并找到他们正在扩展的类的所有单例对象( MonthDay )。

解决方案

我的解决方案基于 Landei的答案将是:

  trait MyEnum {
def valsOfType [T:Manifest] = {
val c = implicitly [Manifest [T]]。erasure
for {m< - getClass.getMethods
如果m.getParameterTypes.isEmpty&&& c.isAssignableFrom(m.getReturnType)
} yield(m.invoke(this).asInstanceOf [T])
}
}

class MonthDay(month: Int,day:Int)

对象MonthDay扩展MyEnum {
//也许你想调用这个假期代替
lazy val values = valsOfType [MonthDay]

val NewYear = new MonthDay(1,1)
val UnityDay = new MonthDay(11,9)
val圣尼古拉斯=新月日(12,6)
val圣诞节=新月月(12,24)
}

我不认为你应该叫这个 MyEnum ,因为枚举类型意味着一组封闭的值。



(如果枚举不起作用值定义为对象 s)


How can I get all of the objects within an object with reflection?

Consider this code:

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

I want to have a trait (MyEnum) which I can mix into the object holding my "enumeration objects" with methods to return a list of them (def values: List[MonthDay]) or iterate over them (def next: MonthDay or def previous: MonthDay) without repeating myself a few times (this is absolutely crucial!).

The idea is that values accesses the MonthDay object and finds all singleton objects of the class they are extending (MonthDay) with reflection.

解决方案

My solution, based on Landei's answer would be:

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

I don't think you should call this MyEnum anymore, because an enumerated type implies a closed set of values.

(Doesn't work if the enumeration values are defined as objects)

这篇关于如何通过在反射中混合特征来访问对象内的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆