简体   繁体   English

实现多个通用接口

[英]Implementing multiple generic interfaces

I need to handle events of two different types but I'm running into the following issue: 我需要处理两种不同类型的事件,但是遇到了以下问题:

The interface EventListener cannot be implemented more than once with different arguments: EventListener<PriceUpdate> and EventListener<OrderEvent> . 接口EventListener不能使用不同的参数多次实现: EventListener<PriceUpdate>EventListener<OrderEvent>

Is there an elegant solution for this? 是否有一个优雅的解决方案?

public interface EventListener <E> {
    public void handle(E event);
}
public interface PriceUpdateEventListener extends EventListener<PriceUpdate> {
}
public interface OrderEventListener extends EventListener<OrderEvent> {
}

public class CompositeListener implements OrderEventListener,PriceUpdateEventListener {
....
}

There is only one handle(Object) method in reality. 实际上,只有一个handle(Object)方法。 You are effectively write the same as 您有效地写成与

public class CompositeListener implements EventListener {
    public void handle(Object event) {
        if (event instanceof PriceUpdate) {
            ///
        } else if (event instanceof OrderEvent) {
            ///
        }
    }
}

Without this checking logic, you can't effectively call your event listener in any case. 没有这种检查逻辑,您将无法在任何情况下有效地调用事件监听器。

I'm trying to do the same thing in a project of mine, and there don't seem to be any elegant way to do it. 我正在尝试在我的项目中做同样的事情,而且似乎没有任何优雅的方法可以做到这一点。 The problem is that all the different versions of the generic interface methods have the same name and can have the same argument applied to them. 问题在于,通用接口方法的所有不同版本都具有相同的名称,并且可以对它们应用相同的参数。 At least if you are using subclasses, and as there is no guarantee that you won't, it won't compile. 至少如果您使用子类,并且不能保证您不会使用子类,则它不会编译。 At least that's what I think is happening. 至少那是我认为正在发生的事情。

class Fraction extends Number{
...
}
GenericInteface <T> {
void method(T a);
}

NumberInterface extends GenericInteface <Number>{
}
FractionInterface extends GenericInteface <Fraction>{
}
ClassWithBoth implements NumberInterface, FractionInterface{
void method(Number a){
}
void method(Fraction a){
}}

In this example, if something is calling ClassWithBoth's method named method with an argument that is a Fraction, it have to methods 2 chose from, both would work as a Fraction is also a Number. 在此示例中,如果某人用一个参数作为分数来调用ClassWithBoth的名为method的方法,则必须从方法2中进行选择,因为分数和Number都可以使用。 Doing something like this is stupid but there is no guarante that people don't, and in case they do java will have no idea what to do. 这样做是愚蠢的,但是没有保证人们不会这样做,如果他们这样做,java将不知道该怎么做。

The "Solution" I have come up with is to re-name the functions, like this. 我想出的“解决方案”就是这样重命名功能。

class Fraction extends Number{
...
}
GenericInteface <T> {
void method(T a);
}
NumberInterface {
void numberMethod(Number a);
}
FractionInterface {
void fractionMethod(Fraction a);
}
ClassWithBoth implements NumberInterface, FractionInterface{
void numberMethod(Number a){
}
void fractionMethod(Fraction a){
}}

Sadly that kinda eliminates the hole point of having the GenericInterface in the first place, as u can't really use it. 遗憾的是,这有点消除了最初使用GenericInterface的麻烦,因为您无法真正使用它。

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

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