所以我之前问过这个问题,但我在代码中遇到了一个错误,大多数人都接受了这个问题,而不是问题本身.
无论如何,我试图覆盖类中的接口方法.但是,我希望覆盖方法中的参数类型是overriden方法中定义的参数类型的子类.
界面是:
public interface Observer {
public void update(ComponentUpdateEvent updateEvent) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
虽然覆盖此方法的类是:
public class ConsoleDrawer extends Drawer {
//...
@Override
public void update(ConsoleUpdateEvent updateEvent) throws Exception {
if (this.componentType != updateEvent.getComponentType()) {
throw new Exception("ComponentType Mismatch.");
}
else {
messages = updateEvent.getComponentState();
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
ConsoleUpdateEvent是ComponentUpdateEvent的子类.
现在,我可以让ConsoleDrawer中的update()方法将ComponentUpdateEvent作为参数,然后将其转换为ConsoleUpdateEvent,但如果可能的话,我正在寻找一个更优雅的解决方案.任何帮助,将不胜感激.谢谢.