是'Box <Cat>``Box <?的子类型?扩展动物>`?

Fre*_*ind 2 java generics type-systems

Java代码:

class Animal {}
class Cat extends Animal {}
class Box<T> {}

public class Test {
    public void hello(Box<? extends Animal> box) {}
}


Box<Animal> box1 = new Box<Animal>();
Box<Cat> box2 = new Box<Cat>();
new Test().hello(box1);
new Test().hello(box2);
Run Code Online (Sandbox Code Playgroud)

Liskov替换原则,因为box2类型Box<Cat>可以在需要类型的地方使用Box<? extends Animal>,我可以说:

Box<Cat> is subtype of Box<? extends Animal>

其实:我不是,如果甚至不确定Box<? extends Animal>? extends Animal有型

Sot*_*lis 5

Java语言规范状态

给定泛型类型声明C<F1,...,Fn>(n> 0),参数化类型的直接超类型C<T1,...,Tn>(其中Ti (1 ? i ? n)是类型)是以下所有:

  • [...]
  • C<S1,...,Sn>,其中Si包含Ti (1 ? i ? n)(§4.5.1).

关于含有上述

A型参数T1被说成包含另一种类型的自变量T2,写入T2 <= T1,如果设定的表示为类型T2可证明是该组由表示类型的子集T1自反和传递闭包的以下规则(其中,<:表示子类型(§4.10)) :

? extends T <= ? extends S if T <: S

? extends T <= ?

T <= T

T <= ? extends T

[...] // see these if you are interested
Run Code Online (Sandbox Code Playgroud)

在你的情况下,

Box<Cat>
Run Code Online (Sandbox Code Playgroud)

我们关心Cat.有了上面,我们可以说? extends Cat包含Cat.然后我们可以说? extends Animal包含? extends Cat,因为Cat <: Animal.所以? extends Animal包含Cat.因此Box<? extends Animal>是参数化类型的直接超类型Box<Cat>

因此,Box<Cat>是一个子类型Box<? extends Animal>.