This Scala tutorial令我困惑;在Node
抽象类型似乎并不以下多态性的传统规则...Scala抽象类型和多态性
type Node <: NodeIntf // NodeIntf is assignable to Node.
abstract class NodeIntf {
def connectWith(node: Node): Edge
}
class NodeImpl extends NodeIntf {
def connectWith(node: Node): Edge = {
val edge = newEdge(this, node) // NodeImpl (this) is assignable to NodeIntf.
edges = edge :: edges
edge
}
}
protected def newEdge(from: Node, to: Node): Edge
如果Node = NodeIntf
和NodeIntf = NodeImpl
,那么我们为什么不能做Node = NodeImpl
?我问,因为显然上面的代码不会编译 - 为什么必须使用'自我类型引用'? (see tutorial)