java - 但我确实重写了那个抽象方法

标签 java abstract-class

我一定是个白痴,但我只是不明白我的错误是什么。

final class EmptyBench extends MicroBench {
    long doIterations(long numIterations) throws InterruptedException {
        return numIterations;
    }
}

这扩展了包含此声明的 MicroBench 类:

abstract long doIterations(long numIterations) throws InterruptedException;

我收到此错误:

EmptyBench.java:6: error: EmptyBench is not abstract and does not override abstract method doIterations(long) in MicroBench

看不到泛型,返回类型没有变化, throws 子句也没有变化。有人可以告诉我我在这里缺少什么吗?

最佳答案

使用这些类,我可以重现问题:

package a;

public abstract class A {
    abstract void method();
}

package b;

import a.A;

public final class B extends A {
    void method() {}
}

但是,我的 IDE (Eclipse) 给了我不同的错误消息,所以 YMMV:

This class must implement the inherited abstract method A.method(), but cannot override it since it is not visible from B. Either make the type abstract or make the inherited method visible

我没有任何内容可以添加到此错误消息中,只是如果您不控制 MicroBench 类,那么您就不走运了。

顺便说一句,如果 B 是抽象的而不是最终的,那么您可以创建一个类 a.C extends b.B ,该类又能够实现 a.A.method()

关于java - 但我确实重写了那个抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25610331/

相关文章:

java - 方法有8个参数,大于7个授权

c# - 接口(interface)与抽象类

java - 如何找到字符串中的最后一个单词

java - 可克隆和 WebDriver ( Selenium )

java - Spring Batch 将步骤分开在不同的类中

java - 为什么没有抽象方法的情况下将类标记为抽象

java - 当导入已经在父类(super class)中时,为什么我必须在子类中使用导入?

c# - 创建抽象类的对象。怎么可能

java - 为什么不覆盖而不是使用抽象类?

java - JTextArea 不会出现在具有 null 布局的 JPanel 上