问题描述
我正在从 DotNet 转向 Java,这个扩展的想法是新的.
I'm moving over to java from DotNet and this idea of
我看过一些使用 List
与 List
与 List
,但我猜想在泛型中使用和不使用扩展之间没有区别.
I've seen some posts that fully explain using 这是真的吗?如果使用抽象类作为父类,答案会改变吗? Is that true? Would the answer change if using an abstract class as the parent? 对比 创建子类如下 Creates child classes as follows 对比 我猜你在谈论在类型参数声明中使用扩展.在这种情况下: I'm guessing you're talking about the use of 有一个称为 has a bounded type parameter called 有一个无界类型参数,称为 has an unbounded type parameter called 对此进行扩展:假设第二个声明是 To expand on that: imagine if the second declaration were 在您编辑的代码中,将 In the code you've edited in, change 你会看到不同之处.如果您真的尝试使用第二个版本中的类型参数 and you'll see the difference. If you actually try to do something using the type parameter 这真的很长,但我也想指出,所有这些基本上都与您问题的前半部分无关.像 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 这篇关于使用 < 和有什么区别?扩展一些抽象>与 Java 泛型中的 SomeAbstract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!List<? vs.
List<? vs.
List<SomeAbstract>
, but I'm guessing that there is no difference between using, and not using,class My_Abstract
class My_Abstract<SomeAbstract>
编辑
EDIT
class My_Child
class My_Child
推荐答案
class My_Abstract<T
T
的有界类型参数,它必须是 SomeAbstract
或它的某个子类型.T
that must be SomeAbstract
or some subtype of it.class My_Abstract<SomeAbstract>
SomeAbstract
,它可以是任何东西.请注意,SomeAbstract
不再指第一个示例使用的实际类型 SomeAbstract
!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
...它可以被称为E
或Bob
或SomeAbstract
.在所有这些情况下,它仍然只是一个类型参数......实际类型永远不会去那里,也没有任何意义(类型参数的全部意义是不引用特定类型,而是而是允许在创建类的实例时将其他类型放在它的位置).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
的声明更改为My_Child
's declaration toclass My_Child
SomeAbstract
做某事,您还会发现无法调用在真正的 SomeAbstract
类中声明的任何方法.这是一个很好的例子,说明为什么你应该始终遵循使用单字母类型参数的约定......如果你不这样做真的很混乱.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
和 ? 不用于类型参数声明(例如定义泛型类时使用的那些),它们主要用于方法参数.
List
是解释为什么需要通配符的典型示例,因为它作为对象容器的性质使其相对容易理解,但与它们相关的规则适用于任何泛型类型.我试图在 this answer一>.? and
? 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.