泛型——类型通配符
来源:互联网 发布:法律家法律数据库 编辑:程序博客网 时间:2024/06/05 07:16
通过一个例子来逐步说明通配符的使用。
假设有一个需求:定义一个方法可以迭代给定的List集合,被迭代的集合通过参数传入。
List集合是带有泛型参数的,如果没有加泛型参数有可能出现泛型警告,所以考虑给上面的List集合加上泛型实参。但是泛型实参是什么类型呢??
因为List集合中可以存储任何类型的元素,所以考虑使用Object。
尝试2:使用Object作为泛型实参
修改后的代码肯定不会出现泛型警告了,但在调用时却出现了问题,看如下代码:
编译失败:
这是因为,虽然Object是Date、String、Integer、Double的父类,但是 List<Object> 不是 List<Date>、List<String>、List<Integer>、List<Double> 的父类,因而不能把 List<Date> 等类型的对象赋给 List<Object> 类型的变量。
尝试3:使用类型通配符
设定类型通配符的上限
注意:
编译失败:
假设有一个需求:定义一个方法可以迭代给定的List集合,被迭代的集合通过参数传入。
尝试1:不使用泛型形参
public static void iteratorList(List list){Iterator it = list.iterator();while(it.hasNext()){System.out.println(it.next());}}
List集合是带有泛型参数的,如果没有加泛型参数有可能出现泛型警告,所以考虑给上面的List集合加上泛型实参。但是泛型实参是什么类型呢??
因为List集合中可以存储任何类型的元素,所以考虑使用Object。
尝试2:使用Object作为泛型实参
修改后的iteratorList方法如下:
public static void iteratorList(List<Object> list){Iterator it = list.iterator();while(it.hasNext()){System.out.println(it.next());}}
修改后的代码肯定不会出现泛型警告了,但在调用时却出现了问题,看如下代码:
public static void main(String[] args){List<Date> dateList = new ArrayList<>();List<String> strList = new ArrayList<>();List<Integer> intList = new ArrayList<>();List<Double> doubleList = new ArrayList<>();iteratorList(dateList);iteratorList(strList);iteratorList(intList);iteratorList(doubleList);}
编译失败:
error: incompatible types: List<Date> cannot be converted to List<Object> iteratorList(dateList); ^error: incompatible types: List<String> cannot be converted to List<Object> iteratorList(strList); ^error: incompatible types: List<Integer> cannot be converted to List<Object> iteratorList(intList); ^error: incompatible types: List<Double> cannot be converted to List<Object> iteratorList(doubleList); ^
这是因为,虽然Object是Date、String、Integer、Double的父类,但是 List<Object> 不是 List<Date>、List<String>、List<Integer>、List<Double> 的父类,因而不能把 List<Date> 等类型的对象赋给 List<Object> 类型的变量。
尝试3:使用类型通配符
那怎么做才能既消除泛型警告,又能迭代所有的List集合呢??使用类型通配符。如下代码所示:
public static void iteratorList(List<?> list){Iterator it = list.iterator();while(it.hasNext()){System.out.println(it.next());}}
设定类型通配符的上限
假设我们又有一个需求,定义一个方法,可以迭代List集合,但集合的元素只能是Number及其子类,集合通过参数传入。
直接上代码吧。
public static void iteratorList(List<? extends Number> list){Iterator it = list.iterator();while(it.hasNext()){System.out.println(it.next());}}
注意:
使用类型通配符时,只能从集合中取元素,不能向集合中添加元素,否则编译失败。如下代码所示:
public static void iteratorList(List<?> list){list.add("1");}
编译失败:
error: no suitable method found for add(String) list.add("1"); ^ method Collection.add(CAP#1) is not applicable (argument mismatch; String cannot be converted to CAP#1) method List.add(CAP#1) is not applicable (argument mismatch; String cannot be converted to CAP#1) where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?
0 0
- 泛型——类型通配符
- Java泛型——类型通配符<?> 与 类型通配符上限<? extends Number>
- java泛型程序设计——通配符类型+通配符的超类型限定
- 通配符类型
- java_泛型,设置类型通配符的上限
- 【java泛型】类型变量和通配符
- JavaSE_8系列博客——Java语言的特性(六)--泛型(3)--泛型和类型通配符
- struts2—使用通配符
- java-类型通配符
- java 类型通配符
- java--通配符类型
- 8.8 通配符类型
- java 类型通配符 ?
- 泛型类以及类型通配符
- 详解Java泛型(四)之通配符类型
- 泛型[6]声明变量类型时使用泛型通配符
- struts2——配置文件通配符!!!
- linux学习笔记—通配符
- IOS之 基本动画原理
- android 单选按钮 RadioGroup RadioButton
- Callable和Future
- java得到当月天数
- BroadcastReceiver类和实现短信窃听
- 泛型——类型通配符
- UVA - 699 The Falling Leaves
- C++输入函数的用法和区别
- UVA - 10954 Add All
- 百度2015校招机器学习笔试题
- sprintf,你知道多少?
- PHP阅读书籍
- hdu 1231
- 第5周项目1 电阻的串联