问题描述
所以我之前问过这个问题,但是我在代码中犯了一个错误,大多数人都是这样认为的,而不是问题本身。无论如何,我试图覆盖类中的一个接口方法。但是,我希望覆盖方法中的参数类型是overriden方法中定义的参数类型的子类。
接口是:
public interface Observer {
public void update(ComponentUpdateEvent updateEvent)throws Exception;
}
重写此方法的类为:
public class ConsoleDrawer扩展Drawer {
// ...
@Override
public void update(ConsoleUpdateEvent updateEvent)throws Exception {
if(this.componentType!= updateEvent.getComponentType()){
抛出新的异常(ComponentType Mismatch。);
}
else {
messages = updateEvent.getComponentState();
}
}
// ...
}
ConsoleUpdateEvent是ComponentUpdateEvent的子类。
现在,我可以让ConsoleDrawer中的update()方法将ComponentUpdateEvent作为参数,然后将其转换为ConsoleUpdateEvent,但我正在寻找更多如果可能,优雅的解决方任何帮助,将不胜感激。谢谢。
你不能。这不是埃菲尔。问题是你可以使用接口来调用不兼容类型的实现方法。所以协变参数是不允许的。逆变参数也是不允许的,但是更容易提供过载。允许Covariant返回类型(自1.5开始)。
您可以对接口进行参数化:
public interface Observer< T extends ComponentEvent> {
无效更新(T事件)抛出异常;
}
或者,使用更有意义的界面:
public interface ConsoleObserver {
void update(ConsoleEvent event)throws Exception;
}
So I asked this question before but I had a mistake in the code which most people picked up on, rather than the problem itself.
Anyway, I'm trying to override an interface method in a class. However, I want the type of the parameter in the overriding method to be a subclass of the type of the parameter as defined in the overriden method.
The interface is:
public interface Observer {
public void update(ComponentUpdateEvent updateEvent) throws Exception;
}
While the class that overrides this method is:
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();
}
}
//...
}
ConsoleUpdateEvent is a subclass of ComponentUpdateEvent.
Now, I could just have the update() method in ConsoleDrawer take a ComponentUpdateEvent as a parameter and then cast it to a ConsoleUpdateEvent but I'm looking for a slightly more elegant solution if possible. Anyhelp would be appreciated. Thank you.
You can't. This is not Eiffel. The problem being that you could use the interface to call the implementation method with an incompatible type. So covariant parameters are not allowed. Contravariant parameters aren't allowed either, but it is easier to provide an overload. Covariant return type is allowed (since 1.5).
You could parameterise the interface:
public interface Observer<T extends ComponentEvent> {
void update(T event) throws Exception;
}
Alternatively, use a more meaningful interface:
public interface ConsoleObserver {
void update(ConsoleEvent event) throws Exception;
}
这篇关于Java:在过滤方法时替换参数的子类/子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!