循环继承和接口——A类不能实现B类接口而B类实现A接口 [英] Cyclic inheritance and interfaces - class A can't implement class B interface while class B implements A interface
问题描述
我有:
public class A implements BListener {
public interface AListener {}
}
public class B implements AListener {
public interface BListener {}
}
所以,如果我理解正确,循环继承的发生是因为:
So, if I understand correctly the cyclic inheritance happens because:
编译器去A并说嘿,A实现了BListener,我们去找BListener吧!"
然后当它试图找到 BListener 时,它最终找到了 B,它说:
Then when it tries to find BListener, it eventually gets to B, which it says:
嘿,BListener,A 需要的在 B 里面!但是等等!B 需要 AListener!我们去找 AListener!"
然后它到达 A,重复.我做对了吗?
And then it gets to A, repeat. Did I get this right?
顺便说一句,这个编译错误发生在我在Android开发上.
By the way, this compilation error happened to me on Android development.
推荐答案
画出来可能会有所帮助.
It may help to draw it out.
>A
is part of / inherits
V
AListener BListener
^
inherits / is part of
B<
一个可爱的圆圈.如果其他人已经存在,您将无法创建其中之一.
A lovely circle. You can't create one of them without the others already existing.
编译器是否是一只患有多动症的松鼠,它喝咖啡追逐自己的尾巴?
Is the compiler a squirrel with ADHD high on coffee chasing it's own tail?
不,因为松鼠不会停止(直到咖啡因用完).编译器寻找这个然后放弃.
Nope because a the squirrel would not stop (until the caffeine ran out). The compiler looks for this and then gives up.
注意:Eclipse 有一个允许此设置的错误.
Note: Eclipse has a bug which allows this setup.
这篇关于循环继承和接口——A类不能实现B类接口而B类实现A接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!