类不是抽象的,不会覆盖 Java 中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16554801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:24:02  来源:igfitidea点击:

Class is not Abstract and does not Override error in Java

javaoverridingabstract

提问by Phil

I am getting a compile time error with Java:

我在使用 Java 时遇到编译时错误:

MyClass is not abstract and does not override abstract method
onClassicControllerRemovedEvent(
wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent)
in wiiusejevents.utils.WiimoteListener)

Here is the class:

这是课程:

import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;


public class MyClass implements WiimoteListener{

    public void onButtonsEvent(WiimoteButtonsEvent arg0) {
        System.out.println(arg0);
        if (arg0.isButtonAPressed()){
            WiiUseApiManager.shutdown();
        }
    }

    public void onIrEvent(IREvent arg0) {
        System.out.println(arg0);
    }

    public void onMotionSensingEvent(MotionSensingEvent arg0) {
        System.out.println(arg0);
    }

    public void onExpansionEvent(ExpansionEvent arg0) {
        System.out.println(arg0);
    }

    public void onStatusEvent(StatusEvent arg0) {
        System.out.println(arg0);
    }

    public void onDisconnectionEvent(DisconnectionEvent arg0) {
        System.out.println(arg0);
    }

    public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
        System.out.println(arg0);
    }

    public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
        System.out.println(arg0);
    }

    public static void main(String[] args) {
        Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
        Wiimote wiimote = wiimotes[0];
        wiimote.activateIRTRacking();
        wiimote.activateMotionSensing();
        wiimote.addWiiMoteEventListeners(new MyClass());
    }
}

Can I get a better explanation of what this error means?

我能更好地解释这个错误的含义吗?

回答by Tips48

When you implement an Interface you must implement all the methods in that interface. You didn't implement onClassicControllerRemovedEvent.

当您实现一个接口时,您必须实现该接口中的所有方法。你没有实现onClassicControllerRemovedEvent.

回答by zw324

Your class implements an interface WiimoteListener, which has a method onClassicControllerRemovedEvent. However, the methods in interfaces are abstract, which means they are essentially just contracts with no implementations. You need to do one of the things here:

你的类实现了一个接口WiimoteListener,它有一个方法onClassicControllerRemovedEvent。然而,接口中的方法是abstract,这意味着它们本质上只是没有实现的契约。您需要在此处执行以下操作之一:

  1. Implement this method and all the other methods that this interface declares, which make your class concrete, or
  2. Declare your class abstract, so it cannot be used to instantiate instances, only used as a superclass.
  1. 实现此方法和此接口声明的所有其他方法,这些方法使您的类具体,或
  2. 声明您的类是抽象类,因此它不能用于实例化实例,只能用作超类。

回答by Shadow Man

It appears that WiimoteListeneris an interface which defines an onClassicControllerRemovedEventmethod. Your class must define all methods that an interface declares or it will not compile without errors.

看来这WiimoteListener是一个定义onClassicControllerRemovedEvent方法的接口。你的类必须定义接口声明的所有方法,否则它不会在没有错误的情况下编译。

It may also be that this class was designed using a different version of the WiimoteListenerinterface (based on an older or newer version of the jar that includes that interface) and that version did not declare the above mentioned method. If so, it may just require building against the version of the jar that your class was made to use.

也可能是这个类是使用不同版本的WiimoteListener接口设计的(基于包含该接口的 jar 的旧版本或新版本),并且该版本没有声明上述方法。如果是这样,它可能只需要针对您的类使用的 jar 版本进行构建。

回答by Eric Leschinski

How to reproduce that error as simply as possible:

如何尽可能简单地重现该错误:

Java code:

爪哇代码:

package javaapplication3;
public class JavaApplication3 {
    public static void main(String[] args) {
    }
}
class Cat implements Animal{

}

interface Animal{
    abstract boolean roar();
}

Shows this compile time error:

显示此编译时错误:

Cat is not abstract and does not override abstract method roar() in Animal

Why won't it compile?

为什么不编译?

Because:

因为:

  1. You created a class Cat which implements an interface Animal.
  2. Your interface called Animal has an abstract method called roar which must be overridden.
  3. You didn't provide for method roar. There are many ways to eliminate the compile time error.
  1. 您创建了一个实现接口 Animal 的 Cat 类。
  2. 您的名为 Animal 的接口有一个名为 roar 的抽象方法,必须重写该方法。
  3. 你没有提供方法咆哮。有很多方法可以消除编译时错误。

Remedy 1, have Cat override the abstract method roar()

补救措施 1,让 Cat 重写抽象方法 roar()

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
        System.out.println(c.roar());
    }
}
class Cat implements Animal{ 
    public boolean roar(){
        return true;
    }
}

interface Animal{
    abstract boolean roar();
}

Remedy 2, change Cat to be an abstract like this:

补救措施2,把Cat改成这样的抽象:

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c;
    }
}
abstract class Cat implements Animal{ 

}
interface Animal{
    abstract boolean roar();
}

Which means you can't instantiate Cat anymore.

这意味着您不能再实例化 Cat。

Remedy 3, have cat stop implementing Animal

补救措施 3,让 cat 停止执行 Animal

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
    }
}
class Cat{ 

}

interface Animal{
    abstract boolean roar();
}

Which makes roar() no longer a contract for things that animals must know how to do.

这使得 roar() 不再是动物必须知道如何做的事情的契约。

Remedy 3, extend a class rather than implementing an interface

补救措施3,扩展一个类而不是实现一个接口

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
        System.out.println(c.roar());
    }
}
class Cat extends Animal{ 

}
class Animal{
    boolean roar(){
        return true;
    }
}

The remedy to use depends on what the best model is to represent the problem being represented. The error is there to urge you stop "programming by brownian motion".

要使用的补救措施取决于代表所代表问题的最佳模型。错误在于敦促您停止“通过布朗运动进行编程”。

回答by JumpMan

Missing params, inconsistent param types, missing method definitions, check all of this out.

缺少参数、不一致的参数类型、缺少方法定义,请检查所有这些。

In my case:

就我而言:

public class California {
            @override
            public String transportation(String transportationType, String transportationId, String transportationArea)
        {
        return transportationType;
    } public static void main(String[] args) {
                California c = new California();
            }
        }

        interface Oakland{
           String transportation(String transportationType, String transportationId);
        }

This did not compile because transportation method missed one of the params!

这没有编译,因为运输方法错过了一个参数!