java 16-8 泛型高级之通配符


  泛型高级(通配符)
      ?:任意类型,如果没有明确,那么就是Object以及任意的Java类了
      ? extends E:向下限定,E及其子类
      ? super E:向上限定,E极其父类

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 public class GenericDemo {
 4 public static void main(String[] args) {
 5 // 泛型如果明确的写的时候,前后必须一致
 6 Collection<Object> c1 = new ArrayList<Object>();
 7 // Collection<Object> c2 = new ArrayList<Animal>();
 8 // Collection<Object> c3 = new ArrayList<Dog>();
 9 // Collection<Object> c4 = new ArrayList<Cat>();
10 
11 // ?表示任意的类型都是可以的
12 Collection<?> c5 = new ArrayList<Object>();
13 Collection<?> c6 = new ArrayList<Animal>();
14 Collection<?> c7 = new ArrayList<Dog>();
15 Collection<?> c8 = new ArrayList<Cat>();
16 
17 // ? extends E:向下限定,E及其子类
18 // Collection<? extends Animal> c9 = new ArrayList<Object>();
19 Collection<? extends Animal> c10 = new ArrayList<Animal>();
20 Collection<? extends Animal> c11 = new ArrayList<Dog>();
21 Collection<? extends Animal> c12 = new ArrayList<Cat>();
22 
23 // ? super E:向上限定,E极其父类
24 Collection<? super Animal> c13 = new ArrayList<Object>();
25 Collection<? super Animal> c14 = new ArrayList<Animal>();
26 // Collection<? super Animal> c15 = new ArrayList<Dog>();
27 // Collection<? super Animal> c16 = new ArrayList<Cat>();
28 }
29 }
30 
31 class Animal {
32 }
33 
34 class Dog extends Animal {
35 }
36 
37 class Cat extends Animal {
38 }

 

转载于:https://www.cnblogs.com/LZL-student/p/5898317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值