有什么用?在公共静态中T addAndReturn(T 元素, Collection集合){
当我遇到这种通用方法时,我感到很困惑。
public static <T> T addAndReturn(T element, Collection<T> collection){
collection.add(element);
return element;
}
我无法理解为什么此方法需要
。
另外,泛型方法和使用泛型语法的方法有什么区别?
I get confused when I come across a generic method of this sort.
public static <T> T addAndReturn(T element, Collection<T> collection){
collection.add(element);
return element;
}
I cannot understand why <T>
is required in this method.
Also, what is the difference between generic methods and the methods that use generics syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(在尖括号中)称为泛型类型参数,而直接位于方法名称前面的T
是返回类型。您可能有一个泛型方法,该方法返回泛型类型以外的其他类型。也许您想要一个将元素添加到集合中的方法(因此是T
类型的对象),而不是返回添加的对象(也可能是T
类型) ),您希望它返回集合中该项目的索引,该索引始终是 int。返回类型不是泛型类型的方法签名示例:
泛型方法与任何其他方法一样,可以具有任何返回类型,包括泛型类型本身、任何其他类类型、任何基本或固有数据类型或 void。您可能会混淆方法签名的两个不相关的部分,认为它们是相关的,但实际上并非如此。它们只是“依赖”,因为如果您确实使用 T 作为返回类型,则返回值的类型是您在调用站点提供的泛型类型。
The
<T>
(in angle brackets) is known as the generic type parameter, whereas theT
directly preceeding the method name is the return type. You might have a generic method that returns some other type than the generic type. Perhaps you want a method that adds an element to a collection (so an object of typeT
), and rather than returning the added object (which would also be of typeT
), you want it to return the index of that item in the collection, which would always be an int.Example method signature where the return type is not the generic type:
Generic methods, like any other method, can have any return type, including the generic type itself, any other class type, any basic or inherent data type, or void. You may be confusing 2 unrelated parts of the method signature, thinking they are dependent, when they are not. They are only "dependent" in the sense that if you do use T as the return type, the return value's type is of the generic type you provide at the call site.
另一种方法是使用 object 而不是 T,这会导致函数返回 object。
当它返回 T 时,您可以执行以下操作:
如果 addAndReturn 返回对象,则必须使用
需要显式强制转换的语句,或者
需要两个语句而不是一个语句的语句。
编辑:既然您的问题已经格式化,我看到参数之一是
Collection
。在这种情况下,通用语法确保返回编译时错误而不是运行时错误。
编辑:以防万一您的问题是关于
语法而不是一般泛型的使用:
告诉编译器方法定义中使用的T
不是某个类T,而是“适合”该方法的特定调用的任何类的占位符。The alternative, using object instead of T, would cause the function to return object.
When it returns T, you can do something like:
If addAndReturn returned object instead, you would have to use either
which needs an explicit cast, or
which needs two statements instead of one.
EDIT: Now that your question has been formatted, I see that one of the parameters is
Collection<T>
. In that case, the generic syntax ensures thatreturns a compile-time error instead of a run-time error.
EDIT: And just in case your question was about the
<T>
syntax rather than about the use of generics in general: The<T>
tells the compiler that theT
used in the method definition is not some class T but rather a placeholder for whatever class "fits" for a specific call of the method.也就是说,泛型 T 只是返回的更具体类型的占位符。在您提供的情况下,T 是您要添加到集合中的元素的类型。
由于它们都是 T 而不是 T1 或 T2,因此它们必须是相同类型的对象,因为显然需要单一类型,因为函数将类型添加到泛型集合
That is a generic T is simply a placeholder for the more concrete type that is returned. In the case you supplied T is the type of element you are adding to the collection.
Since they are all T rather than T1 or T2 they must be the same type of object as a single type is required obviously since the function adds a type to a generic collection
如果可以这么说的话,使用 T 是出于多态原因。 T 是类 Type 的任何类型。
也就是说,例如,如果您有一个 Customer 类,您可以使用上述方法来处理您的用户类型 Customer 类。 T 允许您放置任何您想要的类型。
T 将允许您将此泛型方法用于您自己的用户定义类型,就像它对值类型或任何其他类型所做的那样。 T 成为元素参数中指定的类型。
T is used for polymorphic reasons, if we may say so. T is any type of class Type.
That said, if you come to have a Customer class, for instance, you may use this above mentioned method to process your user type Customer class. T allows you to place in any type you wish.
T will allow you to use this generic method for your own user defined types, just like it will do for value types or any other types. T becomes the type specified in the element parameter.
此方法返回相同的输入
元素
,可能是因为实现者打算使用Fluent Interface< /a> 类似的方式来提供更具可读性的 API:这里 你有一个很好的教程我认为关于 Java 泛型的内容是完整且具有启发性的。
This method returns the same input
element
probably because implementers intended to use a Fluent Interface like way to provide a more readable API:Here you have a nice tutorial on Java generics that I find complete and enlightening.