我无法让这段代码正常工作。我想制作一个特征,允许继承它的类有“孩子”,但显然,Child
'ssetParent
方法想要 a P
,但得到 a Parent[P, C]
。
package net.fluffy8x.thsch.entity
import scala.collection.mutable.Set
trait Parent[P, C <: Child[C, P]] {
protected val children: Set[C]
def register(c: C) = {
children += c
c.setParent(this) // this doesn't compile
}
}
trait Child[C, P <: Parent[P, C]] {
protected var parent: P
def setParent(p: P) = parent = p
}