使用 < 和有什么区别?扩展一些抽象>与 Java 泛型中的 SomeAbstract [英] What is the difference between using <? extends SomeAbstract> vs. SomeAbstract in java generics

查看:26
本文介绍了使用 < 和有什么区别?扩展一些抽象>与 Java 泛型中的 SomeAbstract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 DotNet 转向 Java,这个扩展的想法是新的.

I'm moving over to java from DotNet and this idea of extends is new.

我看过一些使用 ListListList,但我猜想在泛型中使用和不使用扩展之间没有区别.

I've seen some posts that fully explain using List<? extends SomeAbstract> vs. List<? super SomeAbstract> vs. List<SomeAbstract>, but I'm guessing that there is no difference between using, and not using, extends in generics.

这是真的吗?如果使用抽象类作为父类,答案会改变吗?

Is that true? Would the answer change if using an abstract class as the parent?

class My_AbstractExtends<T extends SomeAbstract>

对比

class My_Abstract<SomeAbstract>

<小时>

编辑

创建子类如下


EDIT

Creates child classes as follows

class My_ChildExtends extends My_AbstractExtends<ConcreteChildOfSomeAbstract>

对比

class My_Child extends My_Abstract<ConcreteChildOfSomeAbstract>

推荐答案

我猜你在谈论在类型参数声明中使用扩展.在这种情况下:

I'm guessing you're talking about the use of extends in type parameter declarations. In that case:

class My_Abstract<T extends SomeAbstract>

有一个称为 T 的有界类型参数,它必须是 SomeAbstract 或它的某个子类型.

has a bounded type parameter called T that must be SomeAbstract or some subtype of it.

class My_Abstract<SomeAbstract>

有一个无界类型参数,称为 SomeAbstract,它可以是任何东西.请注意,SomeAbstract 不再指第一个示例使用的实际类型 SomeAbstract

has an unbounded type parameter called SomeAbstract that could be anything. Note that SomeAbstract no longer refers to the actual type SomeAbstract that the first example uses at all!

对此进行扩展:假设第二个声明是 class My_Abstract.T 显然是一个类型参数,而不是一个实际的类型.但它不一定要被称为T...它可以被称为EBobSomeAbstract.在所有这些情况下,它仍然只是一个类型参数......实际类型永远不会去那里,也没有任何意义(类型参数的全部意义是不引用特定类型,而是而是允许在创建类的实例时将其他类型放在它的位置).

To expand on that: imagine if the second declaration were class My_Abstract<T>. The T there is clearly a type parameter, not an actual type. But it doesn't have to be called T... it can be called E or Bob or SomeAbstract. In all of these cases, it is still just a type parameter... an actual type can never go there, nor does it make any sense for it to (the whole point of a type parameter is to NOT refer to a specific type but instead allow other types to be put in its place when instances of the class are created).

在您编辑的代码中,将My_Child 的声明更改为

In the code you've edited in, change My_Child's declaration to

class My_Child extends My_Abstract<Object>

你会看到不同之处.如果您真的尝试使用第二个版本中的类型参数 SomeAbstract 做某事,您还会发现无法调用在真正的 SomeAbstract 类中声明的任何方法.这是一个很好的例子,说明为什么你应该始终遵循使用单字母类型参数的约定......如果你不这样做真的很混乱.

and you'll see the difference. If you actually try to do something using the type parameter SomeAbstract in the second version, you'd also find that you cannot call any methods declared in the real SomeAbstract class. This is all a good example of why you should always follow the convention of using single-letter type parameters... it's really confusing if you don't.

这真的很长,但我也想指出,所有这些基本上都与您问题的前半部分无关.像 这样的通配符?扩展 SomeAbstract?super SomeAbstract 不用于类型参数声明(例如定义泛型类时使用的那些),它们主要用于方法参数.List 是解释为什么需要通配符的典型示例,因为它作为对象容器的性质使其相对容易理解,但与它们相关的规则适用于任何泛型类型.我试图在 这个答案.

This is getting really long, but I also want to note that all of this is basically unrelated to the first half of your question. Wildcards like ? extends SomeAbstract and ? super SomeAbstract aren't used in type parameter declarations (such as those used when defining a generic class), they're primarily used for method parameters. List is a typical example for explaining why wildcards are needed because its nature as a container of objects makes it relatively easy to understand, but rules pertaining to them apply to any generic type. I tried to explain this in relatively general terms in this answer.

这篇关于使用 &lt; 和有什么区别?扩展一些抽象>与 Java 泛型中的 SomeAbstract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆