javac 是否应该查找同名匿名类之外的方法?
这个问题是以下问题的后续: 为什么可以我不是在同名的匿名类之外调用方法 上一个
问题回答为什么,但现在我想知道 javac 应该 是否找到 run (int栏)? (请参阅上一个问题以了解 run(42) 失败的原因)
如果不应该,是否是由于规范所致? 它会产生不明确的代码吗? 我的观点是,我认为这是一个错误。 虽然上一个问题解释了为什么此代码无法编译,但我认为如果 javac 在当前级别找不到匹配项,并且在树中的更高位置进行搜索,则应该编译它。 IE。 如果 this.run() 不匹配,它应该自动检查 NotApplicable.this 的 run 方法。
另请注意, 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) {
}
}
This question is a follow up to:
Why can’t I call a method outside of an anonymous class of the same name
This previous question answer why, but now I want to know if javac should find run(int bar)? (See previous question to see why run(42) fails)
If it shouldn't, is it due to a spec? Does it produce ambiguous code? My point is, I think this is a bug. While the previous question explained why this code fails to compile, I feel it should compile if javac searched higher in the tree if it fails to find a match at the current level. IE. If this.run() does not match, it should automatically check NotApplicable.this for a run method.
Also note that foo(int bar) is correctly found. If you give any reason why run(int bar) shouldn't be found, it must also explain why foo(int bar) is found.
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) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说,这听起来像是一个导致歧义和脆弱的秘诀——一旦在基类中添加了新方法(好吧,对于接口来说不太可能......),代码的含义就会完全改变。
匿名类已经相当丑陋了 - 使这一点明确一点也不困扰我。
Sounds like a recipe for ambiguity and fragility to me - as soon as a new method is added in your base class (okay, not so likely for an interface...) the meaning of your code changes completely.
Anonymous classes are pretty ugly already - making this bit of explicit doesn't bother me at all.
尝试
一下。
Try
instead.
javac 的这种行为符合规范。 请参见§15.12 方法调用表达式 Java 语言规范,特别是“编译时步骤 1”下的段落解释了非限定方法调用的含义:
换句话说,在所有封闭范围中搜索非限定方法 name,以及最里面的“类型声明”(意味着类或接口声明)其中找到的名称就是将搜索整个签名的名称(在“编译时步骤 2”中)。
This behavior of javac conforms to the spec. See §15.12 Method Invocation Expressions in the Java Language Specification, specifically the paragraph under "Compile Time Step 1" explaining the meaning of an unqualified method invocation:
In other words, the unqualified method name is searched for in all enclosing scopes, and the innermost "type declaration" (which means either a class or an interface declaration) in which the name is found is the one that will be searched for the whole signature (in "Compile Time Step 2").