循环继承和接口 - 当B类实现A接口时,A类不能实现B类接口 [英] Cyclic inheritance and interfaces - class A can't implement class B interface while class B implements A interface

查看:193
本文介绍了循环继承和接口 - 当B类实现A接口时,A类不能实现B类接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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.


编译器是否是咖啡追逐它自己的尾巴时ADHD高的松鼠?

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.

这篇关于循环继承和接口 - 当B类实现A接口时,A类不能实现B类接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆