[英]Generic declaration in method
What does the generic type declaration <T>
right before the return type of the get
method in the following code example mean? 在以下代码示例中,恰好在
get
方法的返回类型之前的泛型类型声明<T>
是什么意思?
public interface Listener<T, R> {
<T> Listener<T, R> get();
}
I understand its meaning in the class declaration of Listener
but I did not find a practical example using <T>
in that way for a method. 我在
Listener
的类声明中理解了它的含义,但没有找到以这种方式使用<T>
的实际示例。
It is a method scoped parameterized type. 它是方法范围内的参数化类型。 It is defined only in the scope of the
get()
method and it hides the T
declared in the Listener
class. 它仅在
get()
方法的范围内定义,并且隐藏了在Listener
类中声明的T
Hiding type is something to avoid and defining a method scoped parameterized type in a generic class is often questionable. 隐藏类型是要避免的事情,在泛型类中定义方法范围的参数化类型通常是有问题的。
I want to know what is the role of the parameter in that place.
我想知道该参数在那个地方的作用。
Here T
is a type inferred by the client of the method. 这里
T
是由该方法的客户端推断出的类型。
But as this T
is distinct of the T
declared in the class, it can be a distinct type. 但是由于此
T
与类中声明的T
不同,因此它可以是不同的类型。
If you want to convert the T
of the generic class to another type specified by the client it could make sense. 如果要将泛型类的
T
转换为客户端指定的另一种类型,则可能有意义。 For example suppose the implementation : 例如,假设实现:
public class MyListener implements Listener<String, Integer>{
@Override
public <T> Listener<T, Integer> get() {
return convert to T and return a new instance of Listener...;
}
}
So you could write : 所以你可以这样写:
MyListener myListener = new MyListener();
Listener<Float, Integer> listener = myListener.get();
Note that the returned Listener
and MyListener
are not compatible types. 请注意,返回的
Listener
和MyListener
不是兼容类型。
I would add that your interface is actually a functional interface as it declares a single abstract instance method. 我要补充一点,您的接口实际上是一个功能接口,因为它声明了一个抽象实例方法。
Using generic method doesn't allow to use functional interface with lambdas body but only with method references. 使用泛型方法不允许将函数接口与lambdas主体一起使用,而只能与方法引用一起使用。
For example these could not compile : 例如,这些无法编译:
Listener<String, Integer> invalidListener = () -> new MyListener<String, Integer>();
Listener<String, Integer> invalidListener2 = () -> new MyListener<>();
while the method reference will compile : 而方法参考将编译:
Listener<String, Integer> validListener = MyListener::new;
This is a method specific parametrized type, but... There is already defined T
and R
parameters in the interface so this should be avoided since it hides the defined parameter from the interface. 这是方法特定的参数化类型,但是...接口中已经定义了
T
和R
参数,因此应避免这种情况,因为它从接口隐藏了已定义的参数。
The correct usage example would be in the case the interface defines only parameter R
: 正确的用法示例是接口仅定义参数
R
:
public interface Listener<R> {
<T> Listener<R> get(T t);
}
However, even this usage is questionable. 但是,即使这种用法也值得怀疑。 The example is clear - the interface works with parameter
R
and only its method uses T
additionally. 这个例子很清楚-接口使用参数
R
并且仅其方法另外使用T
Another method without this <T>
singature would not be able to use T
. 没有
<T>
特征的另一种方法将无法使用T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.