有什么用?在公共静态中T addAndReturn(T 元素, Collection集合){

发布于 2024-08-10 01:18:17 字数 263 浏览 1 评论 0原文

当我遇到这种通用方法时,我感到很困惑。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴情 2024-08-17 01:18:17
> public static <T> T addAndReturn(T
> element, Collection<T> collection){
>     collection.add(element);
>     return element; }

(在尖括号中)称为泛型类型参数,而直接位于方法名称前面的 T 是返回类型。您可能有一个泛型方法,该方法返回泛型类型以外的其他类型。也许您想要一个将元素添加到集合中的方法(因此是 T 类型的对象),而不是返回添加的对象(也可能是 T 类型) ),您希望它返回集合中该项目的索引,该索引始终是 int。

返回类型不是泛型类型的方法签名示例:

public static <T> Integer addAndReturn(T element, Collection<T> collection)

泛型方法与任何其他方法一样,可以具有任何返回类型,包括泛型类型本身、任何其他类类型、任何基本或固有数据类型或 void。您可能会混淆方法签名的两个不相关的部分,认为它们是相关的,但实际上并非如此。它们只是“依赖”,因为如果您确实使用 T 作为返回类型,则返回值的类型是您在调用站点提供的泛型类型。

> public static <T> T addAndReturn(T
> element, Collection<T> collection){
>     collection.add(element);
>     return element; }

The <T> (in angle brackets) is known as the generic type parameter, whereas the T 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 type T), and rather than returning the added object (which would also be of type T), 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:

public static <T> Integer addAndReturn(T element, Collection<T> collection)

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.

心奴独伤 2024-08-17 01:18:17

另一种方法是使用 object 而不是 T,这会导致函数返回 object。

当它返回 T 时,您可以执行以下操作:

addAndReturn(myElement, col).SomeMethodOfMyElement();

如果 addAndReturn 返回对象,则必须使用

((MyElementType)addAndReturn(myElement, col)).SomeMethodOfMyElement();

需要显式强制转换的语句,或者

addAndReturn(myElement, col);
myElement.SomeMethodOfMyElement;

需要两个语句而不是一个语句的语句。


编辑:既然您的问题已经格式化,我看到参数之一是 Collection。在这种情况下,通用语法确保

addAndReturn(mySquare, collectionOfCircles);

返回编译时错误而不是运行时错误。


编辑:以防万一您的问题是关于 语法而不是一般泛型的使用: 告诉编译器方法定义中使用的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:

addAndReturn(myElement, col).SomeMethodOfMyElement();

If addAndReturn returned object instead, you would have to use either

((MyElementType)addAndReturn(myElement, col)).SomeMethodOfMyElement();

which needs an explicit cast, or

addAndReturn(myElement, col);
myElement.SomeMethodOfMyElement;

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 that

addAndReturn(mySquare, collectionOfCircles);

returns 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 the T 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.

零時差 2024-08-17 01:18:17

也就是说,泛型 T 只是返回的更具体类型的占位符。在您提供的情况下,T 是您要添加到集合中的元素的类型。

Collection<int> intCollection = new Collection();
int addedInt = addAndReturn(5, intCollection);
addedInt == 5;

由于它们都是 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.

Collection<int> intCollection = new Collection();
int addedInt = addAndReturn(5, intCollection);
addedInt == 5;

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

北方。的韩爷 2024-08-17 01:18:17

如果可以这么说的话,使用 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.

燕归巢 2024-08-17 01:18:17

此方法返回相同的输入元素,可能是因为实现者打算使用Fluent Interface< /a> 类似的方式来提供更具可读性的 API:

addAndReturn(myElement, col).doElementStuff().doOtherElementStuff();

这里 你有一个很好的教程我认为关于 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:

addAndReturn(myElement, col).doElementStuff().doOtherElementStuff();

Here you have a nice tutorial on Java generics that I find complete and enlightening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文