Generics, Inheritance, and Subtypes
本文最后更新于 2024年3月19日 上午
As you already know, it is possible to assign an object of one type to an object of another type provided that the types are compatible. For example, you can assign an Integer to an Object, since Object is one of Integer’s supertypes:
1 |
|
在面向对象的术语中,这称为“是”关系。由于 Integer 是 Object 的一种,因此允许赋值。但 Integer 也是 Number 的一种,因此以下代码也是有效的:
1 |
|
The same is also true with generics. You can perform a generic type invocation, passing Number as its type argument, and any subsequent invocation of add will be allowed if the argument is compatible with Number:
1 |
|
Now consider the following method:
1 |
|
What type of argument does it accept? By looking at its signature, you can see that it accepts a single argument whose type is Box<Number>
. But what does that mean? Are you allowed to pass in Box<Integer>
or Box<Double>
, as you might expect? The answer is “no”, because Box<Integer>
and Box<Double>
are not subtypes of Box<Number>
.
Given two concrete types A and B (for example, Number and Integer),
MyClass<A>
has no relationship toMyClass<B>
, regardless of whether or not A and B are related. The common parent ofMyClass<A>
andMyClass<B>
is Object.
Generic Classes and Subtyping
You can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.
Using the Collections classes as an example, ArrayList<E>
implements List<E>
, and List<E>
extends Collection<E>
. So ArrayList<String>
is a subtype of List<String>
, which is a subtype of Collection<String>
. So long as you do not vary the type argument, the subtyping relationship is preserved between the types.
Now imagine we want to define our own list interface, PayloadList, that associates an optional value of generic type P
with each element. Its declaration might look like:
1 |
|
The following parameterizations of PayloadList are subtypes of List<String>
:
1 |
|