java - 是否存在带有类型参数的泛型(generics with generics)?

标签 java generics higher-kinded-types

我需要定义一个通用总线接口(interface),它可以是 CommandBusQueryBus

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/

相关文章:

c# - 在通用中查找速度

java - java中如何调用泛型类型的方法?

具有更高种类类型和方差的 scala 类型类

scala - 在 Scala 中,为什么我不能为类型构造函数使用上下文绑定(bind)?

java protected 方法可访问性

java - Struts2 jquery 选项卡式面板超时不触发事件

java - 如何在android中替换双引号内的字符

java - 抽象类 - 为什么我的 protected 方法可以公开访问?

java - Java 中泛型的静态多态性

haskell - Haskell 有善统一吗?