class MyClass1 {
int x = 10;
public static void main(String[] args) {
MyClass1 obj = new MyClass1();
obj.execute();
}
private void execute() {
Thread t = new Thread(new Runnable(){
@Override
public void run() {
System.out.println(this);
System.out.println(MyClass1.this.x);
}
});
t.start();
}
}
這裏指的是匿名內部類的對象。這就是爲什麼this.x不起作用。但是我們如何使用這個來引用MyClass1對象?請解釋。當我們執行Sysout(this)時,它會輸出[email protected],其中$指定內部類對象。我不清楚這一點。引用匿名內部類中的實例變量
你'this'是匿名的Runnable類,而不是MyClass1的。 –
@HovercraftFullOfEels不應該爲靜態主類中的類成員變量工作或? – SomeJavaGuy
@SomeJavaGuy:完全正確。需要。更多。咖啡! –