2014-04-10 59 views

回答

4

我试图解释你的图表中的代码如下:

interface A extends B, C, D { } 
interface B { } 
interface C { } 
interface D extends F{ } 
interface E extends F{ } 
interface F extends A{ } 

,编译器失败,错误:

java: cyclic inheritance involving A 

所以,正如所料,循环继承是不允许的。

错误消失如果F确实不是延伸A

2

它确实产生一个错误,你尝试过吗?

interface A extends D {} // error on this line: The hierarchy of the type A is inconsistent 
interface D extends F {} // error on this line: The hierarchy of the type D is inconsistent 
interface F extends A {} // error on this line: Cycle detected: a cycle exists in the type hierarchy between F and A 
0

错误是在d扩展F和E扩展F和F延伸的... 由于Java不支持多重继承...这就是为什么接口被引入

+1

它确实支持多接口继承。问题在于循环。 –

1

我看到下面的疯狂用法:

interface I { 
    static final I CONSTANT = new C(); 
    ... 
} 

class C implements I { 
} 

这个工作的原因是.class(就像一个二进制.obj)包含其导入的符号信息。所以.java可以被编译成.class。然后可以完成与其他.class文件的链接。

出于同样的原因(松散链接)执行可能会失败,当使用不同版本的jar进行编译而不是运行时。

虽然有些编译器可能会遇到特定的周期。


一个另外虽然

如果一个接口使用常量从另一个接口,在编译时这些常量存储在.class文件,并且在效果消失的进口。在那种情况下,至少有一个编译器,它不检测依赖关系,并且周期可能被破坏。这甚至可能导致错误的,陈旧的常量;反模式。