Scala在子类中限定受保护的成员

时间:2016-04-27 12:04:08

标签: scala protected

Scala Language Spec.我找到了

不同形式的资格受到保护[this]。标记有此修饰符的成员M称为对象保护;它只能从定义它的对象中访问。也就是说,选择p.M仅在前缀为this或O.this时才合法,对于包含引用的某些类O.此外,对不合格保护的限制适用

但我了解 的情况 例如 的 this.protectedMember

但我没有得到的是

O.this,对于某个包含引用的类O

请帮助..

然而,正如我的理解所说,这是内部类的内容,就像我们在 Swing 中获取外部类对象一样,例如 匿名内部类中的 OuterClass.this.someMethod

1 个答案:

答案 0 :(得分:1)

  

The expression C.this is legal in the statement part of an enclosing class or object definition with simple name C. It stands for the object being defined by the innermost such definition.

E.g。你可以

class O {
  class I {
    // O.this is the instance of O this I instance is nested in
    def M = ...
    M // calls M in I
    O.this.M // calls M in O
  }

  protected[this] def M = ...
}
相关问题