<?超级T>和<?在Java中扩展T>

<?超级T>和<?在Java中扩展T>

.之间的区别是什么?List<? super T>List<? extends T> ?

我以前经常用List<? extends T>,但它不允许我向它添加元素。list.add(e),而List<? super T>的确如此。


侃侃无极
浏览 439回答 3
3回答

慕标5832272

1。延展通过写作&nbsp; &nbsp; List<? extends C2> list;你是说list将能够引用类型的对象(例如)ArrayList其泛型类型为7之一亚型的C2 (C2包括):C2: new ArrayList<C2>();,(可存储C2或子类型的对象)或D1: new ArrayList<D1>();,(可存储D1或子类型的对象)或D2: new ArrayList<D2>();,(可以存储D2或子类型的对象)或.诸若此类。七个不同的案件:&nbsp; &nbsp; 1) new ArrayList<C2>(): can store C2 D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 2) new ArrayList<D1>(): can store&nbsp; &nbsp; D1&nbsp; &nbsp; E1 E2&nbsp;&nbsp;&nbsp; &nbsp; 3) new ArrayList<D2>(): can store&nbsp; &nbsp; &nbsp; &nbsp;D2&nbsp; &nbsp; &nbsp; &nbsp;E3 E4&nbsp; &nbsp; 4) new ArrayList<E1>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; E1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 5) new ArrayList<E2>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;E2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 6) new ArrayList<E3>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; E3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 7) new ArrayList<E4>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;E4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;对于每一种可能的情况,我们都有一组“可存储的”类型:这里用图形表示的7组(红色)集。enter image description here正如你所看到的,没有一个安全型这在每一种情况下都是常见的:你不能list.add(new C2(){});因为它可能是list = new ArrayList<D1>();你不能list.add(new D1(){});因为它可能是list = new ArrayList<D2>();诸若此类。2。超级通过写作&nbsp; &nbsp; List<? super C2> list;你是说list将能够引用类型的对象(例如)ArrayList其泛型类型为7之一超型的C2 (C2包括):A1: new ArrayList<A1>();,(可存储A1或子类型的对象)或A2: new ArrayList<A2>();,(可存储A2或子类型的对象)或A3: new ArrayList<A3>();,(可以存储A3或子类型的对象)或.诸若此类。七个不同的案件:&nbsp; &nbsp; 1) new ArrayList<A1>(): can store A1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B1 B2&nbsp; &nbsp; &nbsp; &nbsp;C1 C2&nbsp; &nbsp; D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 2) new ArrayList<A2>(): can store&nbsp; &nbsp; A2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B2&nbsp; &nbsp; &nbsp; &nbsp;C1 C2&nbsp; &nbsp; D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 3) new ArrayList<A3>(): can store&nbsp; &nbsp; &nbsp; &nbsp;A3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B3&nbsp; &nbsp; &nbsp; &nbsp;C2 C3 D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 4) new ArrayList<A4>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A4&nbsp; &nbsp; &nbsp; &nbsp;B3 B4&nbsp; &nbsp; C2 C3 D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 5) new ArrayList<B2>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B2&nbsp; &nbsp; &nbsp; &nbsp;C1 C2&nbsp; &nbsp; D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 6) new ArrayList<B3>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;B3&nbsp; &nbsp; &nbsp; &nbsp;C2 C3 D1 D2 E1 E2 E3 E4&nbsp; &nbsp; 7) new ArrayList<C2>(): can store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2&nbsp; &nbsp; D1 D2 E1 E2 E3 E4对于每一种可能的情况,我们都有一组“可存储的”类型:这里用图形表示的7组(红色)集。enter image description here如你所见,这里有七个安全类型这在每一种情况下都是常见的:C2, D1, D2, E1, E2, E3, E4.你可以的list.add(new C2(){});因为,不管我们指的是哪种名单,C2被允许你可以的list.add(new D1(){});因为,不管我们指的是哪种名单,D1被允许诸若此类。您可能注意到,这些类型对应于从类型开始的层次结构。C2.注记这里是完整的层次结构,如果您想做一些测试interface A1{}interface A2{}interface A3{}interface A4{}interface B1 extends A1{}interface B2 extends A1,A2{}interface B3 extends A3,A4{}interface B4 extends A4{}interface C1 extends B2{}interface C2 extends B2,B3{}interface C3 extends B3{}interface D1 extends C1,C2{}interface D2 extends C2{}interface E1 extends D1{}interface E2 extends D1{}interface E3 extends D2{}interface E4 extends D2{}

炎炎设计

我手里拿着一个X。如果我想写我的X变成一个列表,这个列表需要是一个X的列表,或者是我的X在写它们时可以向上转换的东西的列表。超类X的.。List<? super   X>如果我得到一份清单,我想朗读,阅读从这个列表中取出来的X,最好是X的列表,或者在我读出来的时候可以向上转换成X的东西的列表,也就是延展 XList<? extends X>希望这能帮上忙。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java