Java泛型 - 方法签名的上限[重复]

时间:2021-10-17 13:09:55

This question already has an answer here:

这个问题在这里已有答案:

I have a method where I want to accept class types that must extend an abstract class. What is the difference between

我有一个方法,我想接受必须扩展抽象类的类类型。有什么区别

<T extends AbstractClass> void myMethod(Class<T> clazz);

void myMethod(Class clazz);

and

void myMethod(Class<? extends AbstractClass> clazz); ?

void myMethod(Class clazz); ?

I wouldn't be able to directly reference the type inside the method in the second case. Is there any difference in which class types could be passed to these two methods?

在第二种情况下,我无法直接引用方法内部的类型。可以将哪些类类型传递给这两种方法有什么区别吗?

3 个解决方案

#1


5  

No, there is no difference between the argument types compatible with the two method signatures you presented. Personally, I would use the parameterized version if I needed to reference the exact type represented by the argument, but otherwise I would use the wildcard version.

不,与您提供的两个方法签名兼容的参数类型之间没有区别。就个人而言,如果我需要引用参数表示的确切类型,我会使用参数化版本,否则我将使用通配符版本。

#2


2  

In the 1st one you will also be able to return T (or any type parameterized with T: List<T>, Set<T> and so on...) , without needing to cast it

在第一个中你也可以返回T(或任何用T参数化的类型:List ,Set 等等......),而无需投射它

<T extends AbstractClass> T myMethod(Class<T> clazz);

And use it as :

并将其用作:

Subclass parameterInstance =... 
Subclass i1 = myMethod(parameterInstance.getClass());

#3


1  

This question has been asked many places, notably here and here.

这个问题已被问到很多地方,特别是在这里和这里。

Both questions deal primarily with unbounded wildcards and generic types but the same principles apply here. I'd also recommend reading one of the links (Angelika Langer - Java Generics FAQs) provided by one of the answers to the other questions (placed here for convenient).

这两个问题主要涉及*通配符和泛型类型,但同样的原则适用于此处。我还建议阅读其中一个答案提供的其中一个链接(Angelika Langer - Java Generics常见问题解答)(这里放置方便)。

Whilst in your specific case there is no difference, the difference comes down simply to how you would be dealing with the type data internally (within the method). Go with what seems to describe your purpose the best. If you are dealing with data of unknown type and you require the specific input type to be specifically usable within the method, you'll need to go with the generics approach. If on the other hand, you do not and can suffice with treating all input data as simply of the bounding type (e.g. AbstractClass in your case) you may go with the bounded wildcard approach.

虽然在您的具体情况下没有区别,但差异仅仅取决于您如何在内部处理类型数据(在方法内)。用似乎最能描述你的目的去做。如果您正在处理未知类型的数据,并且您需要在方法中特定使用特定输入类型,则需要使用泛型方法。另一方面,如果你没有并且只需要将所有输入数据视为边界类型(例如,在你的情况下是AbstractClass)就足够了,你可以使用有界通配符方法。

#1


5  

No, there is no difference between the argument types compatible with the two method signatures you presented. Personally, I would use the parameterized version if I needed to reference the exact type represented by the argument, but otherwise I would use the wildcard version.

不,与您提供的两个方法签名兼容的参数类型之间没有区别。就个人而言,如果我需要引用参数表示的确切类型,我会使用参数化版本,否则我将使用通配符版本。

#2


2  

In the 1st one you will also be able to return T (or any type parameterized with T: List<T>, Set<T> and so on...) , without needing to cast it

在第一个中你也可以返回T(或任何用T参数化的类型:List ,Set 等等......),而无需投射它

<T extends AbstractClass> T myMethod(Class<T> clazz);

And use it as :

并将其用作:

Subclass parameterInstance =... 
Subclass i1 = myMethod(parameterInstance.getClass());

#3


1  

This question has been asked many places, notably here and here.

这个问题已被问到很多地方,特别是在这里和这里。

Both questions deal primarily with unbounded wildcards and generic types but the same principles apply here. I'd also recommend reading one of the links (Angelika Langer - Java Generics FAQs) provided by one of the answers to the other questions (placed here for convenient).

这两个问题主要涉及*通配符和泛型类型,但同样的原则适用于此处。我还建议阅读其中一个答案提供的其中一个链接(Angelika Langer - Java Generics常见问题解答)(这里放置方便)。

Whilst in your specific case there is no difference, the difference comes down simply to how you would be dealing with the type data internally (within the method). Go with what seems to describe your purpose the best. If you are dealing with data of unknown type and you require the specific input type to be specifically usable within the method, you'll need to go with the generics approach. If on the other hand, you do not and can suffice with treating all input data as simply of the bounding type (e.g. AbstractClass in your case) you may go with the bounded wildcard approach.

虽然在您的具体情况下没有区别,但差异仅仅取决于您如何在内部处理类型数据(在方法内)。用似乎最能描述你的目的去做。如果您正在处理未知类型的数据,并且您需要在方法中特定使用特定输入类型,则需要使用泛型方法。另一方面,如果你没有并且只需要将所有输入数据视为边界类型(例如,在你的情况下是AbstractClass)就足够了,你可以使用有界通配符方法。