java-子接口重写SuperInterface方法

java-子接口重写SuperInterface方法,java,interface,Java,Interface,我目前正试图用java编写一个游戏引擎/库,但在创建事件系统时遇到了一个问题。我想要一个全局EventManager类来管理每次更新一次的事件队列。该类不应由用户更改,但应能够使用用户创建的事件侦听器和事件。因此,我有一个抽象类事件,用户可以从中创建子类作为自己的事件类型。用户创建的监听器都应该是EventListener接口的子接口,这样用户就可以将自己的几个监听器实现到一个类中。 由于每个侦听器都有特定的事件类型,应该触发其操作方法,因此必须有一个变量或方法可以告诉EventManager该

我目前正试图用java编写一个游戏引擎/库,但在创建事件系统时遇到了一个问题。我想要一个全局EventManager类来管理每次更新一次的事件队列。该类不应由用户更改,但应能够使用用户创建的事件侦听器和事件。因此,我有一个抽象类事件,用户可以从中创建子类作为自己的事件类型。用户创建的监听器都应该是EventListener接口的子接口,这样用户就可以将自己的几个监听器实现到一个类中。 由于每个侦听器都有特定的事件类型,应该触发其操作方法,因此必须有一个变量或方法可以告诉EventManager该侦听器感兴趣的事件。 有没有一种方法可以让子接口重写超级接口的方法? 或者,对于将侦听器与事件匹配的问题,还有其他好的解决方案吗

以下是迄今为止我制作的类和接口的部分:

EventManager类:

import java.util.ArrayList;

public class EventManager {

private static ArrayList<Event> eventQueue = new ArrayList<Event>();
private static ArrayList<EventListener> listeners = new ArrayList<EventListener>();

public static void queueEvent(Event e){
    eventQueue.add(e);
}

public static void instantEvent(Event e){
    for(int i = 0; i < eventQueue.size(); i++){
        for(int l = 0; l < listeners.size(); l ++){
            if(listeners.get(l).getEventTypes().contains(e))listeners.get(l).trigger(e);
        }
    }
}

public static void processEventQueue(){
    for(int i = 0; i < eventQueue.size(); i++){
        for(int l = 0; l < listeners.size(); l ++){
            if(listeners.get(l).getEventTypes().contains(eventQueue.get(i)))listeners.get(l).trigger(eventQueue.get(i));
        }
    }
    eventQueue.clear();
}

public static void addListener(Object o){
    if(o instanceof EventListener)listeners.add((EventListener) o);
}

public static void removeListener(Object o){
    if(o instanceof EventListener)listeners.remove((EventListener) o);
}
EventListener超级接口:

import java.util.ArrayList;

public abstract interface EventListener{

public abstract void trigger(Event e);

public abstract ArrayList<Event> getEventTypes();
}
public interface ExampleListener extends EventListener{

@Override
public ArrayList<Event> getEventTypes(){
    //This method isn´t allowed to have a body.
            //----THAT IS THE PROBLEM----
}

}
import java.util.ArrayList;
公共抽象接口EventListener{
公开摘要无效触发器(事件e);
公共抽象ArrayList getEventTypes();
}
侦听器子接口:

import java.util.ArrayList;

public abstract interface EventListener{

public abstract void trigger(Event e);

public abstract ArrayList<Event> getEventTypes();
}
public interface ExampleListener extends EventListener{

@Override
public ArrayList<Event> getEventTypes(){
    //This method isn´t allowed to have a body.
            //----THAT IS THE PROBLEM----
}

}
公共接口示例Listener扩展了EventListener{
@凌驾
公共ArrayList getEventTypes(){
//此方法不允许有正文。
//----这就是问题所在----
}
}

提前谢谢

接口只与契约类似,它们确保类将定义接口中给定的方法。接口的主要用途是,如果您知道一个类实现了一个接口,那么您也知道您将在该类中找到该接口的方法

更具可读性,并支持命名约定


为此,请尝试使用抽象类。

当一个接口扩展另一个接口时,例如在一个类中,它继承了它的所有方法

我理解你的问题,我以前也有过。 不允许在接口上有任何实现。您可以选择将它变成一个抽象类,但是您可能需要扩展多个类,而您不能。 您可能想做的是保留抽象类(实现该接口)的引用,而只是将该引用用于与事件相关的事情。 你也可以通过匿名的方式保存自己:

EventListener evListener = new EventListener(){
  void trigger(Event e){
    //do whatever with event e
  }

  ArrayList<Event> getEventTypes(){
    //return event type
  }
};
EventListener evListener=neweventlistener(){
无效触发器(事件e){
//对事件e做任何事情
}
ArrayList getEventTypes(){
//返回事件类型
}
};

希望有帮助:)

您可以使用java 8默认方法:


接口中的方法是根据定义抽象的。如果你想在这样的层次结构中实现一个方法,那么也许可以考虑抽象类来代替?问题是一个类只能从一个超类继承,但是从多个接口继承…正当我希望有可能有多个侦听器或一个超类加一个侦听器。是的,但不要尝试交换超类和接口。。接口只是一组必须在接口实现的类中定义的方法。我真的不明白你的确切问题,你能给我更确切的信息,关于你计划如何制作类树,你想实现什么?感谢代码块中的解决方案,我将尝试这样做:)。但是你描述了两种解决方案,我不理解另一种。你能给我举个例子吗?好的。代码块上的解决方案意味着您正在初始化EventListener类型的对象并在运行时定义它。另一个实际具有相同效果的解决方案是:公共类ExampleEvent实现EventListener{public void trigger(Event e){//…trigger implementation}公共ArrayList getEventTypes(){//…getEventTypes implementation}这个类现在可以拥有接口本身无法实现的所有实现!现在您可以参考ExampleEvent(它有实现)!知道了?