1 回答
TA贡献1818条经验 获得超3个赞
如果它不是您的抽象类,则可以检查方法的声明类,然后该方法被覆盖。
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
class Main {
static abstract class EventListener {
public void onFileChanged() {
throw new NotImplementedException();
}
}
static class EventListenerNotImpl extends EventListener {
}
static class EventListenerImpl extends EventListener {
private String id;
public EventListenerImpl(String id) {
this.id = id;
}
public void onFileChanged() {
System.out.println(id + ":" + EventListenerImpl.class.getCanonicalName() + ".onFileChanged() was called");
}
}
static class EventHandler {
private List<EventListener> listeners = new ArrayList<>();
public void addListener(EventListener listener) {
listeners.add(listener);
}
private void propagateOnFileChangedEvent() {
listeners.forEach(l -> {
try {
Method m = l.getClass().getMethod("onFileChanged");
if (!m.getDeclaringClass().equals(EventListener.class)) {
l.onFileChanged();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
});
}
}
public static void main(String[] args) {
EventHandler handler = new EventHandler();
handler.addListener(new EventListenerImpl("listener-1"));
handler.addListener(new EventListenerNotImpl()); // Not will be invoked onFileChangedEvent
handler.addListener(new EventListenerImpl("listener-3"));
handler.propagateOnFileChangedEvent();
}
}
输出:
listener-1:Main.EventListenerImpl.onFileChanged() was called
listener-3:Main.EventListenerImpl.onFileChanged() was called
添加回答
举报