Scala中类主体中的右箭头
浏览
Scala源代码,我偶然发现了Enumeration.scala:
abstract class Enumeration(initial: Int,names: String*) extends Serializable { thisenum => def this() = this(0) def this(names: String*) = this(0,names: _*) /* Note that `readResolve` cannot be private,since otherwise the JVM does not invoke it when deserializing subclasses. */ protected def readResolve(): AnyRef = thisenum.getClass.getField("MODULE$").get() // ... SNIP ... } 什么是thisenum =>对于?我在“Scala编程”一书中找不到任何信息. 解决方法
Scala 2d编程中的编程在第29.4节“将模块拆分为特征”一节中介绍了自我类型的概念:
trait SimpleFoods { object Pear extends Food("Pear") def allFoods = List(Apple,Pear) def allCategories = Nil }
trait SimpleRecipes { // Does not compile object FruitSalad extends Recipe( "fruit salad",List(Apple,Pear),// Uh oh "Mix it all together." ) def allRecipes = List(FruitSalad) }
trait SimpleRecipes { this: SimpleFoods => object FruitSalad extends Recipe( "fruit salad",// Now Pear is in scope "Mix it all together." ) def allRecipes = List(FruitSalad) }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |