Java中的循环继承层次结构 [英] Cyclic inheritance hierarchy in Java
问题描述
我知道Java中不允许使用以下循环继承层次结构。编译器抛出错误,但我真正感兴趣的是知道编译失败的确切原因。
I know following cyclic inheritance hierarchy is not allowed in Java. Compiler throws an error, but what I'm really interested is knowing the exact reason for the compilation failure.
class A extends B{}
class B extends C{}
class C extends A{} // this will give you compile time error.
编译器抛出错误的原因是什么,我编写代码的那一刻 C类扩展A {}
What is the thing due to which the compiler will throw an error, the moment I write the code class C extends A{}
推荐答案
这种关系根本不可能。它定义了一个无限递归类。为了定义 class C
,你需要 class A
来定义 class A
您需要 B类
,并定义 B类
您需要类C
- 你回到了起点。这种情况无穷无尽,因此编译器无法做到这一点,也没有逻辑意义。
Such relation is simply not possible. It defines an infinite recursive class. In order to define class C
, you need class A
, to define class A
you need class B
, and to define class B
you need class C
- and you are back to the starting point. This goes on infinitely so compiler can't do this and it also has no logical meaning.
这篇关于Java中的循环继承层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!