简体   繁体   English

下限的通配符

[英]Lower bounded wildcard

I have been trying to understand the concept and rationale behind the lower bounded wildcard in Java Generics. 我一直在努力理解Java Generics中下限通配符背后的概念和基本原理。 I can understand the reasoning behind the upper bounded wildcard being read only and where and how it can be used. 我可以理解上边界通配符背后的推理,以及它在何处以及如何使用。 I am still not able to get a grasp of the lower bounded wildcard. 我仍然无法掌握下限的通配符。 I have a set of classes which follow the below hierarchy. 我有一组遵循以下层次结构的类。 ( Automobile is the base class.) Automobile是基类。)

Automobile
    - Bus
        - Minibus
        - Doubledecker
        - Electricbus
    - Car
        - Sedan
        - Hatchback
        - Coupe
    - Truck
        - Minivan
        - Pickuptruck
        - Suv
            - Fullsuv
            - Midsuv

Now I am trying to build a (flexible) list and add objects of different types to it. 现在我正在尝试构建一个(灵活的)列表并向其添加不同类型的对象。

import java.util.ArrayList;
import java.util.List;

public class Garage {

    public static void main(String[] args)
    {
        List<? super Suv> list = null;

        Suv s = new Suv();
        Truck t = new Truck();
        Automobile a = new Automobile();

        list = new ArrayList<Suv>();
        list.add(s); // declared as List<? super Suv> but actually List<Suv>. No compilation error

        list = new ArrayList<Truck>();
        list.add(t); // declared as List<? super Suv> but actually List<Truck>. Compilation error

        list = new ArrayList<Automobile>();
        list.add(a); // declared as List<? super Suv> but actually List<Automobile>. Compilation error
    }
}

I am able to add an instance of Suv to List<? super Suv> 我能够将一个Suv实例添加到List<? super Suv> List<? super Suv> . List<? super Suv> But I get compilation error when I try to add instances of Truck or Automobile to the same list. 但是当我尝试将TruckAutomobile实例添加到同一列表时,我收到编译错误。 Isn't the List<? super Suv> 不是List<? super Suv> List<? super Suv> flexible enough to take any instances of Suv or its super classes? List<? super Suv>足够灵活,可以采取Suv或其超级类的任何实例? Truck and Automobile are higher up in the hierarchy tree. TruckAutomobile在层次结构树中更高。 Why am I not able to add instances of these to the list? 为什么我无法将这些实例添加到列表中?

List<? super Suv> List<? super Suv> should be read as "A list whose element type is a super type of Suv " . List<? super Suv>应该被理解为“一个元素类型是Suv超类型的列表”

Now, let's look at these two lines: 现在,让我们看看这两行:

list = new ArrayList<Automobile>();
list.add(a); // where a is an Automobile

The first line is ok, since ArrayList<Automobile> is indeed "a list whose element type is a super type of Suv " . 第一行是好的,因为ArrayList<Automobile>确实是“一个元素类型是Suv超类型的列表”

But let's read out the second line: Add some Automobile to a list whose element type is as super type of Suv . 但是让我们读出第二行:将一些Automobile添加到一个列表,其元素类型是超级类型的Suv

As far as the compiler knows from the static types, 就编译器从静态类型中所知,

  • list could refer to a List<Truck> (since Truck is a super type of Suv ) and list可以引用List<Truck> (因为Truck是超级类型的Suv )和
  • t may refer to a Car (since a Car is an Automobile ) t可以指Car (因为CarAutomobile

So the compiler complains to make sure you're not adding a Car to a list of Trucks . 因此,编译器会抱怨确保您没有将Car添加到Trucks列表中。


Useful reading: 有用的阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM