Java泛型方法签名说明 [英] Java generics method signature explanation
问题描述
此通用方法签名是什么意思
What does this generic method signature means
public <S, D> D map(S sourceObject, java.lang.Class<D> destinationClass);
我指的是<S, D>
.
此方法返回与第二个参数中发送的对象相同类型的对象.
I am referring to the <S, D>
.
This method returns an object of the same type that was sent in the second parameter.
我不能只写
public D map(S sourceObject, java.lang.Class<D> destinationClass);
推荐答案
这意味着该方法调用采用两个类型参数:S和D.
That means that this method invocation takes two type parameters: S and D.
<S, D>
部分是为了声明此方法是通用的,并接受两个类型参数,然后将它们用作方法签名中实际类型的占位符.
The <S, D>
part is meant as a declaration that this method is generic and takes two type parameters which are then used as placeholders for actual types in the method signature.
调用该方法时,要么提供参数,要么从您作为参数传递的表达式类型中推断出参数,如下所示:
When you invoke the method, either you supply the parameters or they get inferred from the types of the expressions you are passing as arguments, like this:
String val = map(10, String.class);
在这种情况下,S为Integer
,D为String
In this case S is Integer
and D is String
这篇关于Java泛型方法签名说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!