以下代码段(从实际代码中抽象出来)在Eclipse中编译并运行。
包1 / Outer.java:
package package1;
import package1.Outer.Mid.Inner;
import package2.Bar;
public class Outer {
final Mid mid = new Mid();
public Outer() {
mid.setInner(new Inner() {
@Override public void foo() {
System.out.println("In Outer.foo()");
}
});
}
public static class Mid implements Bar {
private Inner inner;
public void setInner(Inner inner) {
this.inner = inner;
}
public Inner getInner() {
return this.inner;
}
@Override
public void bar() {}
interface Inner {
void foo();
}
}
}
包2 / Bar.java:
package package2;
public interface Bar {
void bar();
}
但是,使用javac进行编译时失败并显示此错误:
package1\Outer.java:31: cannot find symbol
symbol : class Bar
location: class package1.Outer
public static class Mid implements Bar {
^
package1\Outer.java:42: method does not override or implement a method from a supertype
@Override
^
2 errors
现在,如果我切换import语句的顺序,就像这样:
import package2.Bar;
import package1.Outer.Mid.Inner;
...然后它在Eclipse和javac中编译。很明显,导入语句的顺序似乎很重要......但为什么呢?
注意:
package1.Outer.Mid.Inner
导入甚至是必要的,因为Inner
接口嵌套在Outer.java中,但Eclipse和javac似乎都需要它