java - java中的 "qualified this"构造是什么意思?

标签 java jls

Effective Java在“第 22 项:优先考虑静态成员类而不是非静态”项中,Josh Bloch 说:

Each instance of a nonstatic member class is implicitly associated with an enclosing instance of its containing class. Within instance methods of a nonstatic member class, you can invoke methods on the enclosing instance or obtain a reference to the enclosing instance using the qualified this construct.

他说的合格的这个构造是什么意思

最佳答案

没有限定符,x() 将递归。使用限定符,将调用封闭实例的 x() 方法。

class Envelope {
  void x() {
    System.out.println("Hello");
  }
  class Enclosure {
    void x() {
      Envelope.this.x(); /* Qualified*/
    }
  }
}

关于java - java中的 "qualified this"构造是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276994/

相关文章:

java - 为什么序列化后类类型丢失了?

java - 一堆图像的 ImageJ 亮度和对比度

java - Android WifiManager 扫描结果在 Gingerbread 之后给出不一致的数据?

java - 为什么 Java 常量除以零不会产生编译时错误?

java - Java 7 语法中的分隔标识符可以简化为限定标识符吗?

c# - 比较 C# 和 Java

java - Java Applet 重绘问题

关于通配符的 Java 语言规范

eclipse - 这是 eclipse 或 maven-compiler-plugin 错误,还是泛型类转换问题?

java - "variable xxx might not have been initialized"调用静态方法时返回相同类型的变量和类型本身的相同名称