【问题标题】:"The type B cannot be a superinterface of C; a superinterface must be an interface" error“类型 B 不能是 C 的超接口;超接口必须是接口”错误
【发布时间】:2012-05-13 04:30:33
【问题描述】:

假设我得到了这个接口 A:

interface A
{
    void doThis();
    String doThat();
}

所以,我想要一些抽象类来实现方法 doThis() 而不是 doThat() 方法:

abstract class B implements A
{
    public void doThis()
    {
        System.out.println("do this and then "+ doThat());
    }

}

abstract class B2 implements A
{
    public void doThis()
    {
        System.out.println(doThat() + "and then do this");
    }
}

当您最终决定在常规类中实现 de doThat 方法时,会出现错误:

public class C implements B
{
    public String doThat()
    {
        return "do that";
    }
}

这个类导致我出现上述错误:

“类型B不能是C的超接口;超接口必须是接口”

如果这个类的层次结构是有效的,现在任何人都可以,或者我应该做其他方式吗?

【问题讨论】:

标签: java


【解决方案1】:

你必须使用extends

public class C extends B

了解implementsextends 关键字之间的区别很重要。所以,我建议你从这个问题开始阅读:Implements vs extends: When to use? What's the difference? 和那里的答案。

【讨论】:

  • 谢谢!我也忘记将公共抽象 String doThat() 添加到每个抽象类:S
  • @PainyJames 您不需要将public abstract String doThat() 添加到每个抽象类中。
【解决方案2】:

由于B是一个类,正确的语法是使用extends

public class C extends B {

【讨论】:

    【解决方案3】:

    B 是一个抽象类,不能与“implements”关键字一起使用。您必须改用“扩展”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2016-11-21
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多