类似private的Scala语法[this]
我是scala新手,有人能指出下面方括号中的术语是什么吗类似private的Scala语法[this],scala,terminology,Scala,Terminology,我是scala新手,有人能指出下面方括号中的术语是什么吗 private[this]lazy val outputAttributes=AttributeSeq(输出) 谢谢。这叫 标记为private且没有限定符的成员称为class private, 而标有private[this]的成员称为object private 并指定 最严格的访问是标记方法 当您这样做时,该方法仅对 当前对象的当前实例。其他情况 同一类无法访问该方法 更准确地说,[this]私有[this]的一部分称为访问限定符:
private[this]lazy val outputAttributes=AttributeSeq(输出)
谢谢。这叫
标记为private
且没有限定符的成员称为class private,
而标有private[this]
的成员称为object private
并指定
最严格的访问是标记方法
当您这样做时,该方法仅对
当前对象的当前实例。其他情况
同一类无法访问该方法
更准确地说,[this]
私有[this]的一部分称为访问限定符:
private[此]
将隐私进一步提升,并使字段对象私有化。与private
不同,现在该字段不能被相同类型的其他实例访问,这使得它比普通的private设置更私密
比如说,
class Person {
private val name = "John"
def show(p: Person) = p.name
}
(new Person).show(new Person) // result: John
class Person {
private[this] val name = "John"
def show(p: Person) = p.name // compilation error
}
添加private[this]后,该字段只能由当前实例访问,不能由类的任何其他实例访问。请参见此用法示例:
class Person {
private val name = "John"
def show(p: Person) = p.name
}
(new Person).show(new Person) // result: John
class Person {
private[this] val name = "John"
def show(p: Person) = p.name // compilation error
}