简体   繁体   English

Java 使用泛型参数和默认方法实现编译功能接口失败

[英]Java failed to compile Functional Interface with generic parameter and default method implementation

I'm trying to extend this interface:我正在尝试扩展此接口:

@FunctionalInterface
public interface HandlerFunction<T extends ServerResponse> {
    Mono<T> handle(ServerRequest request);
}

Like this:像这样:

@FunctionalInterface
public interface HandlerFn<R extends ServerRequest> extends HandlerFunction<ServerResponse> {
    default Mono<ServerResponse> handle(ServerRequest request) {
        return handleFn(request);
    }

    Mono<ServerResponse> handleFn(R request);
}

I'm getting this error (on handleFn(request) ):我收到此错误(在handleFn(request)上):

The method handleFn(R) in the type HandlerFn<R> is not applicable for the arguments (ServerRequest)

In this code snippet, why when calling handleFn(request) the type R is not inferred as ServerRequest ?在此代码片段中,为什么在调用handleFn(request)时类型R不会被推断为ServerRequest

request is a ServerRequest , not an R . request是一个ServerRequest ,而不是一个R R could be ServerRequest , but it could be any other subclass of it. R可以ServerRequest ,但也可以是它的任何其他子类。

R is declared on the class, not the method. R是在 class 上声明的,而不是方法上。 The type of R is determined at the declaration of the reference to the HandlerFn , not at the call site of the handleFn method. R 的类型是在声明对R的引用时确定的,而不是在HandlerFn方法的调用handleFn

You could cast inside the default method:您可以在默认方法中进行转换:

return handleFn((R) request);

which is an unchecked cast (at that point; it would be checked elsewhere).这是一个未经检查的演员表(那时;它将在其他地方检查)。

If you declared the method as a generic method, it would work in handle :如果您将该方法声明为泛型方法,它将在handle中工作:

<RR extends ServerRequest> Mono<ServerResponse> handleFn(RR request);

(You wouldn't then need R on the interface. I just called it RR to emphasize that it is a separate type variable). (然后你就不需要接口上的R 。我只是称它为RR以强调它是一个单独的类型变量)。

But, in fact, there is no point in such a type variable: this gives no advantage over declaring the method non-generically:但是,事实上,这样的类型变量没有任何意义:与非泛型声明方法相比,这没有任何优势:

Mono<ServerResponse> handleFn(ServerRequest request);

which, of course, has no advantage over the handle method.当然,这与handle方法相比没有优势。

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

相关问题 具有函数式接口参数类型的 Java 泛型方法 - Java Generic on method with Functional Interface parameter type 将泛型类型参数标记为Java 8中的功能接口 - Mark generic type parameter as functional interface in Java 8 在Java中将任何方法作为参数传递的功能接口 - Functional interface to pass any method as parameter in Java 通过功能接口使用Java泛型类型时发生编译错误 - Compile error when using Java generic types with functional interface 将功能接口的实现作为参数传递 - Pass implementation of functional interface as parameter 如何在Java中使用泛型来创建一个泛型接口,该接口定义仅接受实现作为参数的方法? - How would I use generics in Java to create a generic interface that defines a method that only accepts the implementation as a parameter? 在接口中作为通用参数枚举。 在默认实现中使用 - Enum as Generic parameter in Interface. Use in default implementation Java:获取带有接口参数的方法及其实现 - Java: Get method with interface parameter with its implementation 如何在Java 8编译时确保方法签名“实现”功能接口 - How to ensure at Java 8 compile time that a method signature “implements” a functional interface Java - 默认接口方法不能作为其他接口方法的实现 - Java - default interface method cannot act as implementation for other interface method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM