Java泛型二:通配符的使用
原文地址http://blog.csdn.net/lonelyroamer/article/details/7927212
通配符有三种:
1、无限定通配符 形式<?>
2、上边界限定通配符 形式< ? extends Number> //用Number举例
3、下边界限定通配符 形式< ? super Number> //用Number举例
1、泛型中的?通配符
如果定义一个方法,该方法用于打印出任意参数化类型的集合中的所有数据,如果这样写
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- publicclass GernericTest {
- publicstaticvoid main(String[] args) throws Exception{
- List<Integer> listInteger =new ArrayList<Integer>();
- List<String> listString =new ArrayList<String>();
- printCollection(listInteger);
- printCollection(listString);
- }
- publicstaticvoid printCollection(Collection<Object> collection){
- for(Object obj:collection){
- System.out.println(obj);
- }
- }
- }
语句printCollection(listInteger);报错
The method printCollection(Collection<Object>) in the type GernericTest is not applicable for the arguments (List<Integer>)
这是因为泛型的参数是不考虑继承关系就直接报错。
这就得用?通配符
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- publicclass GernericTest {
- publicstaticvoid main(String[] args) throws Exception{
- List<Integer> listInteger =new ArrayList<Integer>();
- List<String> listString =new ArrayList<String>();
- printCollection(listInteger);
- printCollection(listString);
- }
- publicstaticvoid printCollection(Collection<?> collection){
- for(Object obj:collection){
- System.out.println(obj);
- }
- }
- }
在方法public static void printCollection(Collection<?> collection){}中不能出现与参数类型有关的方法比如collection.add();因为程序调用这个方法的时候传入的参数不知道是什么类型的,但是可以调用与参数类型无关的方法比如collection.size();
总结:使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量的主要用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法。
2、泛型中的?通配符的扩展
1:界定通配符的上边界
Vector<? extends 类型1> x = new Vector<类型2>();
类型1指定一个数据类型,那么类型2就只能是类型1或者是类型1的子类
Vector<? extends Number> x = new Vector<Integer>();//这是正确的
Vector<? extends Number> x = new Vector<String>();//这是错误的
2:界定通配符的下边界
Vector<? super 类型1> x = new Vector<类型2>();
类型1指定一个数据类型,那么类型2就只能是类型1或者是类型1的父类
Vector<? super Integer> x = new Vector<Number>();//这是正确的
Vector<? super Integer> x = new Vector<Byte>();//这是错误的
提示:限定通配符总是包括自己
Java泛型二:通配符的使用的更多相关文章
- Java 泛型、通配符? 解惑
Java 泛型通配符?解惑 分类: JAVA 2014-05-05 15:53 2799人阅读 评论(4) 收藏 举报 泛型通配符上界下界无界 目录(?)[+] 转自:http://www.linux ...
- JAVA 泛型与通配符的使用
泛型的本质是参数化类型.即所操作的数据类型被指定为一个参数. 1.jdk 1.5/1.6 必须显式的写出泛型的类型. 2.jdk 1.7/1.8 不必显式的写出泛型的类型. 一.泛型声明 可以用< ...
- Java泛型和通配符那点事
泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...
- java 泛型的通配符和限定
package cn.sasa.demo1; import java.util.ArrayList; import java.util.Collection; import java.util.Ite ...
- Java泛型之通配符
原文点此链接 使用通配符的原因:Java中的数组是协变的,但是泛型不支持协变. 数组的协变 首先了解下什么是数组的协变,看下面的例子: Number[] nums = new Integer[10]; ...
- Java 泛型和通配符解惑
转自:http://www.linuxidc.com/Linux/2013-10/90928.htm T 有类型 ? 未知类型 一.通配符的上界 既然知道List<Cat>并不是Lis ...
- Java 泛型 二
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- java泛型之通配符?
一.在说泛型通配符" ?" 之前先讲几个概念 1.里氏替换原则(Liskov Substitution Principle, LSP): 定义:所有引用基类(父类)的地方必须能透明 ...
- java 泛型与通配符(?)
泛型应用于泛型类或泛型方法的声明. 如类GenericTest public class GenericTest<T> { private T item; public void set( ...
随机推荐
- Mybatis热加载Mapper.xml
开发的时候,写Mybatis Mapper.xml文件的时候,每次修改SQL都需要重启服务,感觉十分麻烦,于是尝试写了一个Mybatis的Mapper.xml热加载. 能在修改Mapper.xml之后 ...
- git版本管理之git-ssh 配置和使用
1.设置用户名和邮箱 $ git config --global user.name "gsx-gh" $ git config --global user.email " ...
- hdu6005 Pandaland 想法+dijkstra
/** 题目:hdu6005 Pandaland 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6005 题意:给定一个带权无向图,求权值和最小的环的值,如 ...
- zabbix 源码安装
操作系统:CentOS IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2.2 LTS 备注:Linux下安装zabbix需要有LA ...
- ubuntu16.0.4 update git
Ubuntu 16.04 comes with Git 2.7.x, which is a little old now. As versions 2.8 & 2.9 are not part ...
- 微信小程序2 - 扩展Page参数
官方默认的Page初始代码为 var option = { /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 * */ onLoad: function ...
- Control character in cookie value or attribute
在cookie中添加中文导致静态页面打不开, (1)先清除缓存 (2)使用escape()函数对中文进行编码,获取的时候在对中文进行解码unescape(). cookie.Set("sto ...
- Linux基础命令(2)
Fskey servername scp命令 grep 命令 find 命令 echo 命令 xargs 命令 file 命令 cat 命令 /dev/null tar 打包 gzip 压缩 示例 ...
- 目标跟踪之卡尔曼滤波---理解Kalman滤波的使用
http://www.cnblogs.com/jcchen1987/p/4371439.html
- ytu 2231: 交集问题(线性表)(数据结构,链表练习)
2231: 交集问题(线性表) Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 6 Solved: 3[Submit][Status][Web Boar ...