我需要定义一个通用总线接口(interface),它可以是 CommandBus
或 QueryBus
。
public interface Bus {
<T> T execute(BusRequest<T> request);
}
public interface BusRequest<T> {
}
上面的示例有效,但我希望将 BusRequest
替换为另一个扩展 BusRequest
的泛型,以便您可以执行以下操作:
public interface CommandBus extends Bus<Command> {
// Inherited method would turn into something like
// <T> T execute(Command<T> cmd);
}
public interface Command<T> extends BusRequest<T> {
}
我如何定义Bus
接口(interface)来做到这一点? Java 中可以吗?
我已经尝试过:
public interface Bus<R extends BusRequest> {
<T> T execute(R<T> request);
}
但是它说:
Type 'R' does not have type parameters
最佳答案
一个Kind ?
没有。为此,您需要 Scala(或 Haskell)。
关于java - 是否存在带有类型参数的泛型(generics with generics)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52915280/