java 类名.this

 

类名.this指该类的实例,主要用在内部类的方法中,要指定某个嵌套层次的外围类的“this”引用时,使用“外围类名.this”语法

 

public class Foo {

    class Bar {
        Foo getFoo() {
            return Foo.this;
        }
    }
}

内部类Bar的方法getFoo()返回外部类Foo的实例

    public static void main(String[] args) {

        Foo foo = new Foo();
        Foo.Bar bar = foo.new Bar();
        System.out.println(foo.getFoo() == bar.getFoo());  // true

    }

 

posted on 2019-08-01 14:22  Deltadeblog  阅读(535)  评论(0编辑  收藏  举报

导航