我目前正在研究如何在Scala中处理协变和逆变类型参数化.还有一个特例,var字段必须是private [this]才能编译.从此代码段中的示例(在此问题中采用:private [this] vs private):
class Holder[+T] (initialValue: Option[T]) {
// without [this] it will not compile
private[this] var value = initialValue
def getValue = value
def makeEmpty { value = None }
}
Run Code Online (Sandbox Code Playgroud)
我理解这个例子.但我无法理解的是,在什么情况下,私有字段可以从其他实例访问而不是itselt(this)?
谢谢你的帮助.
| 归档时间: |
|
| 查看次数: |
486 次 |
| 最近记录: |