对具有 Java 匿名 class 的字段的惊人访问
Surprising access to fields with Java anonymous class
我正在尝试更好地理解 Java 中匿名 classes 的概念。从本网站的其他答案中,我了解到匿名 class 可以使用 OuterClass.this.myField
.
访问封闭 class 的非最终字段
我用一个接口 AnonInt
和一个 class AnonTest
用一个方法 foo
做了一个简单的测试用例,其中 returns 一个实例匿名 class 实施 AnonInt
。尽管我使用的是 System.out.println(a)
而不是 System.out.println(AnonTest.this.a)
代码仍然有效并打印出正确的结果。怎么会这样?
public interface AnonInt {
void print();
}
public class AnonTest {
private int a;
public AnonTest(int a) {
this.a = a;
}
public AnonInt foo() {
return new AnonInt() {
public void print() {
System.out.println(a);
}
};
}
public static void main(String[] args) {
AnonTest at = new AnonTest(37);
AnonInt ai = at.foo();
ai.print();
}
}
Despite the fact that I'm using System.out.println(a)
rather than System.out.println(AnonTest.this.a)
the code works and prints the correct result. How can this be?
由于对 a
的引用在您的上下文中是明确的,因此这两个表达式引用相同的字段。
通常,AnonTest.this
在非常具体的上下文中是必需的 - 当您的方法需要访问 AnonTest
对象本身,而不是访问其成员之一时。对于您的程序,这是不必要的:编译器将表达式 a
解析为对象 AnonTest.this.a
,并将其传递给 System.out.println
.
我正在尝试更好地理解 Java 中匿名 classes 的概念。从本网站的其他答案中,我了解到匿名 class 可以使用 OuterClass.this.myField
.
我用一个接口 AnonInt
和一个 class AnonTest
用一个方法 foo
做了一个简单的测试用例,其中 returns 一个实例匿名 class 实施 AnonInt
。尽管我使用的是 System.out.println(a)
而不是 System.out.println(AnonTest.this.a)
代码仍然有效并打印出正确的结果。怎么会这样?
public interface AnonInt {
void print();
}
public class AnonTest {
private int a;
public AnonTest(int a) {
this.a = a;
}
public AnonInt foo() {
return new AnonInt() {
public void print() {
System.out.println(a);
}
};
}
public static void main(String[] args) {
AnonTest at = new AnonTest(37);
AnonInt ai = at.foo();
ai.print();
}
}
Despite the fact that I'm using
System.out.println(a)
rather thanSystem.out.println(AnonTest.this.a)
the code works and prints the correct result. How can this be?
由于对 a
的引用在您的上下文中是明确的,因此这两个表达式引用相同的字段。
通常,AnonTest.this
在非常具体的上下文中是必需的 - 当您的方法需要访问 AnonTest
对象本身,而不是访问其成员之一时。对于您的程序,这是不必要的:编译器将表达式 a
解析为对象 AnonTest.this.a
,并将其传递给 System.out.println
.