简体   繁体   English

未在实现类中实现的EventListener接口中的handleEvent方法

[英]handleEvent method in EventListener interface not implemented in the implementing class

EventListener interface declares handleEvent(Event evt) method, and in the code below GeneratorListener extends that interface. EventListener接口声明handleEvent(Event evt)方法,然后在GeneratorListener下面的代码中扩展该接口。 I am told this code is correct. 我被告知此代码是正确的。 BUT I don't see why Printer class does not have to implement handleEvent method? 但是我看不到为什么Printer类不必实现handleEvent方法? Isn't it the case that all the methods in an interface must be implemented? 是否必须实现接口中的所有方法?

public interface GeneratorListener extends EventListener {
    void objectGenerated(String object);
}

public class Printer implements GeneratorListener { 
    public void objectGenerated(String object) {
        System.out.println(object);
    }
}

Seems I know the answer. 看来我知道答案了。 Method 方法

public void handleEvent(Event evt);

is contained in org.w3c.dom.events.EventListener. 包含在org.w3c.dom.events.EventListener中。 But in your GeneratorListener interface you probably imported java.util.EventListener that looks like 但是在您的GeneratorListener接口中,您可能导入了看起来像这样的java.util.EventListener

package java.util;

/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}

So your Printer class implements the only abstract method that you have in Printer class hierarchy. 因此,您的Printer类实现了Printer类层次结构中仅有的抽象方法。 That's why your code is correct. 这就是为什么您的代码正确的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM