我正在測試下面的代碼片段,我需要知道如何訪問t.x或t.hello?它的範圍是什麼? 開發者是否以這種方式定義變量?一個實例的局部變量/方法的範圍
public class Test{
public Test(){
System.out.print("constructor\n");
}
public static void main(String[] args) {
Test t = new Test(){
int x = 0;
//System.out.print("" + x);
void hello(){
System.out.print("inside hello\n");
}
};
}
編輯
但是,爲什麼這個片段的工作
Thread tr = new Thread() {
int loops = 1;
@Override
public void run() {
loops += 1;
}
};
tr.start();
有趣的是,我在我的回答中給出的例子非常接近您的編輯;-) – assylias
回覆:您的編輯。 'start()'是'Thread'的一個方法。變量'tr'也是'Thread'類型,所以你可以調用它的方法。只是不是你在AIC中添加的新方法。 – millimoose