gpt4 book ai didi

java - 通用类父

转载 作者:太空狗 更新时间:2023-10-29 22:56:54 25 4
gpt4 key购买 nike

根据Generics trial ,

This section状态:

Given two concrete types A and B (for example, Number and Integer), MyClass<A> has no relationship to MyClass<B>, regardless of whether or not A and B are related. The common parent of MyClass<A> and MyClass<B> is Object.

然而,here我们被告知,

Although Integer is a subtype of Number, List<Integer> is not a subtype of List<Number> and, in fact, these two types are not related. The common parent of List<Number> and List<Integer> is List<?>.

为什么不是 MyClass<A> 的父级/MyClass<B>在第一个例子中 MyClass<?> ?有什么区别?

最佳答案

我认为答案相当微不足道。而 MyClass<A> 的正确父级和 MyClass<B> 确实是 MyClass<?> , 本教程在那里做了一个小的简化,因为通配符还没有被引入。

说的重点

The common parent of MyClass<A> and MyClass<B> is Object.

只是为了表明这两种类型都不是另一种类型的父类型,无论 A 之间的关系如何和 B .

您的第一个引述下方的以下评论证实了这一点:

For information on how to create a subtype-like relationship between two generic classes when the type parameters are related, see Wildcards and Subtyping.

以及由章节介绍Wildcards and Subtyping :

As described in Generics, Inheritance, and Subtypes, generic classes or interfaces are not related merely because there is a relationship between their types. However, you can use wildcards to create a relationship between generic classes or interfaces.

关于java - 通用类父,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com