這個問題的後續行動: Why can’t I call a method outside of an anonymous class of the same name應該由javac在同名的匿名類之外尋找方法嗎?
這前一個問題的答案爲什麼,但現在我想知道,如果javac的應找到運行(INT吧)? (見前面的問題,看看爲什麼運行(42)失敗)
如果它不應該,是由於規範?它會產生不明確的代碼嗎?我的觀點是,我認爲這是一個錯誤。雖然前面的問題解釋了爲什麼這段代碼無法編譯,但我覺得它應該編譯,如果javac在樹中搜索得較高,如果它無法在當前級別找到匹配。 IE瀏覽器。如果this.run()不匹配,它應該自動檢查NotApplicable.this是否有運行方法。
另請注意,正確找到foo(int bar)。如果你給出了找不到run(int bar)的原因,它也必須解釋爲什麼找到foo(int bar)。
public class NotApplicable {
public NotApplicable() {
new Runnable() {
public void run() {
// this works just fine, it automatically used NotApplicable.this when it couldn't find this.foo
foo(42);
// this fails to compile, javac find this.run(), and it does not match
run(42);
// to force javac to find run(int bar) you must use the following
//NotApplicable.this.run(42);
}
};
}
private void run(int bar) {
}
public void foo(int bar) {
}
}
謝謝,我想我理解規範。 現在我可以說這是一個不好的規範,應該改變。編譯時間第1步應包括匿名類的封閉類。 – Pyrolistical 2008-10-31 17:44:19