Java:Class.this
我有一个Java程序,看起来像这样。
public class LocalScreen { public void onMake() { aFuncCall(LocalScreen.this, oneString, twoString); } }
LocalScreen.this
在aFuncCall
LocalScreen.this
意味着什么?
LocalScreen.this
指向封闭类的this
。
这个例子应该解释一下:
public class LocalScreen { public void method() { new Runnable() { public void run() { // Prints "An anonymous Runnable" System.out.println(this.toString()); // Prints "A LocalScreen object" System.out.println(LocalScreen.this.toString()); // Won't compile! 'this' is a Runnable! //onMake(this); // Compiles! Refers to enclosing object onMake(LocalScreen.this); } public String toString() { return "An anonymous Runnable!"; } }.run(); } public String toString() { return "A LocalScreen object"; } public void onMake(LocalScreen ls) { /* ... */ } public static void main(String[] args) { new LocalScreen().method(); } }
输出:
An anonymous Runnable! A LocalScreen object
这篇文章在这里被重写为一篇文章。
这意味着外部LocalScreen
类的this
实例。
this
没有限定符的情况下编写它将返callback用所在的内部类的实例。
编译器接受代码,并用它做这样的事情:
public class LocalScreen { public void method() { new LocalScreen$1(this).run; } public String toString() { return "A LocalScreen object"; } public void onMake(LocalScreen ls) { /* ... */ } public static void main(String[] args) { new LocalScreen().method(); } } class LocalScreen$1 extends Runnable { final LocalScreen $this; LocalScreen$1(LocalScreen $this) { this.$this = $this; } public void run() { // Prints "An anonymous Runnable" System.out.println(this.toString()); // Prints "A LocalScreen object" System.out.println($this.toString()); // Won't compile! 'this' is a Runnable! //onMake(this); // Compiles! Refers to enclosing object $this.onMake($this); } public String toString() { return "An anonymous Runnable!"; } }
正如你所看到的,当编译器接受一个内部类时,它将它转换成一个外部类(这是一个很早以前做出的devise决定,所以不需要改变虚拟机来理解内部类)。
当一个非静态的内部类被创build时,它需要对父类的引用,以便它可以调用外部类的方法/访问variables。
内部类的内部不是正确的types,您需要访问外部类以获取调用onMake方法的正确types。
Class.this
允许访问外部类的实例。 看下面的例子。
public class A { final String name; final B b; A(String name) { this.name = name; this.b = new B(name + "-b"); } class B { final String name; final C c; B(String name) { this.name = name; this.c = new C(name + "-c"); } class C { final String name; final D d; C(String name) { this.name = name; this.d = new D(name + "-d"); } class D { final String name; D(String name) { this.name = name; } void printMe() { System.out.println("D: " + D.this.name); // `this` of class D System.out.println("C: " + C.this.name); // `this` of class C System.out.println("B: " + B.this.name); // `this` of class B System.out.println("A: " + A.this.name); // `this` of class A } } } } static public void main(String ... args) { final A a = new A("a"); abcdprintMe(); } }
那么你会得到。
D: abcd C: abc B: ab A: a
我知道你有什么困惑。我刚才遇到这个问题,应该有特殊的场景来区分它们。
class THIS { def andthen = { new THIS { println(THIS.this.## + ":inner-THIS.this.##") println(this.## + ":inner-this.##") new THIS { println(THIS.this.## + ":inner-inner-THIS.this.##") println(this.## + ":inner-this.##") } } } def getInfo = { println(THIS.this.## + ":THIS.this.##") println(this.## + ":this.##") } }
你可以看到THIS.this
和this
新的THIS操作之间的THIS.this
是hashcode(。##)
在Scala控制台中testing:
scala> val x = new THIS x: THIS = THIS@5ab9b447 scala> val y = x.andthen 1522119751:inner-THIS.this.## 404586280:inner-this.## 1522119751:inner-inner-THIS.this.## 2027227708:inner-this.## y: THIS = THIS$$anon$1@181d7f28 scala> x.getInfo 1522119751:THIS.this.## 1522119751:this.##
THIS.this
总是指向由val x引用的外层THIS类,但是this
超出了匿名的新操作。