简体   繁体   English

Java:在方法中返回未知的泛型类型

[英]Java: Return unknown generic type in method

The method 方法

Filter<T,U> connect(final Filter<T,U> filter) {
...
return filter
}

is in a class 在上课

class Pipe<T>

I get the Error "Cannot resolve U". 我收到错误“无法解析U”。 The idea is to just generically return the same filter with the same two types without knowing the U type. 这个想法是只用相同的两种类型返回相同的过滤器,而无需知道U类型。 How do i do that? 我怎么做?

The goal is to be able to chain without providing type parameters, when they are not necessary because they do not get modified: 目标是能够在不需要提供类型参数的情况下进行链接,而无需提供类型参数,因为它们不会被修改:

(new Pipe<Integer>).connect(new Filter<>(...)).connect(new Pipe<>)...

The second Pipe after the Filter in the above expamle shall implicitly be of the generic type Integer. 上述示例中,紧随过滤器之后的第二个管道应隐式地具有通用类型Integer。

It looks like you are trying to make your method generic . 看来您正在尝试使方法通用 To do this simply add generic type U to its signature right before return type: 为此,只需在返回类型之前将通用类型U添加到其签名中:

<U> Filter<T,U> connect(final Filter<T,U> filter) {
    ...
    return filter
}

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

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