这个问题是跟进:
Why can’t I call a method outside of an anonymous class of the same name
这个前面的问题回答了为什么 b>,但现在我想知道javac是否应该找到run(int bar)? (请参阅上一个问题以了解run(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) {
}
}