在下面的代码中,x的类型是I(虽然x也实现了J但在编译时不知道),为什么(1)处的代码不会导致编译时错误.因为在编译时只考虑引用的类型.
public class MyClass {
public static void main(String[] args) {
I x = new D();
if (x instanceof J) //(1)
System.out.println("J");
}
}
interface I {}
interface J {}
class C implements I {}
class D extends C implements J {}
Run Code Online (Sandbox Code Playgroud)