java泛型(三)
Updated:
参考:
http://blog.csdn.net/lonelyroamer/article/details/7927212#html
http://www.cnblogs.com/mengdd/archive/2013/01/21/2869861.html
初识通配符
  <? extends SomeClass>是一个限界通配符(bounded wildcard),?代表了一个未知的类型,并且它是SomeClass的子类,也可以是SomeClass本身。
  这里面SomeClass是统配符的上界(upper bound of the wildcard)。
  相应的也有限定下界的,使用关键字super。
  通配符所代表的其实是一组类型,但具体的类型是未知的。
类型通配声明
  看下面的代码:1
2
3 GenericFoo<Integer> foo1 = null;
GenericFoo<Boolean> foo2 = null;
//此时foo1只能接受GenericFoo<Integer>类型的实例,foo2只能接受GenericFoo<Boolean>类型的实例
  如果希望有一个变量foo可以指向下面所有的实例:1
2
3//foo = new GenericFoo<ArrayList>();
//foo = new GenericFoo<LinkedList>();
  可以这样声明:1
2
3
4
5GenericFoo<? extends List> foo = null;
foo = new GenericFoo<ArrayList>();
foo = new GenericFoo<LinkedList>();
注意这种形式不同于前面的限制泛型可用类型时提到的形式。
  前面提到的形式
如果该应用指向其他类型,则会编译报错:1
2//Error:Type mismatch
foo = new GenericFoo<HashMap>();
补:运行时类型查询
举个例子:1
ArrayList<String> arrayList=new ArrayList<String>();
  因为类型擦除之后,ArrayList
那么,运行时进行类型查询的时候使用下面的方法是错误的1
2
3
4
5
6if( arrayList instanceof ArrayList<String>)
```    
java限定了这种类型查询的方式
```java
if( arrayList instanceof ArrayList<?>)
通配符有三种:
- 无限定通配符 形式<?>
 - 上边界限定通配符 形式< ? extends Number>
 - 下边界限定通配符 形式< ? super Number>
 
泛型中的?通配符
  如果定义一个方法,该方法用于打印出任意参数化类型的集合中的所有数据,如果这样写1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import java.util.ArrayList;  
import java.util.Collection;  
import java.util.List;  
public class GernericTest {  
    public staticvoid main(String[] args) throws Exception{  
        List<Integer> listInteger =new ArrayList<Integer>();  
        List<String> listString =new ArrayList<String>();  
        printCollection(listInteger);  
        printCollection(listString);      
    }   
    Public static void printCollection(Collection<Object> collection){  
               for(Object obj:collection){  
            System.out.println(obj);  
        }    
    }  
}
  语句printCollection(listInteger);报错
  The method printCollection(Collection
这就得用?通配符
1  | import java.util.ArrayList;  | 
总结1:
在方法public static void printCollection(Collection<?> collection){}中不能出现与参数类型有关的方法比如collection.add();(不过却可以添加null,)因为程序调用这个方法的时候传入的参数不知道是什么类型的,但是可以调用与参数类型无关的方法比如collection.size();
总结2:
使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量的主要用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法。
界定通配符的上边界
1  | Vector<? extends 类型1> x = new Vector<类型2>();  | 
界定通配符的下边界
1  | Vector<? super 类型1> x = new Vector<类型2>();  | 
例://引用指向继承层次之上
   GenericFoo<? super List> ge= null;
   ge = new GenericFoo
提示:限定通配符总是包括自己
补:
GenericFoo<? extends Object>等价于GenericFoo<?>,但是它们与GenericFoo
使用<?>或是<? extends SomeClass>的声明方式,意味着您只能通过该名称来取得所参考的实例的信息,或者是移除某些信息,但不能增加或者改写它的信息。
因为只知道当中放置的是SomeClass的子类,但不确定是什么类的实例,编译器不让您加入信息,理由是,如果可以加入信息的话,那么您就得记得取回的是什么类型的实例,然后转换为原来的类型方可进行操作,这样就失去了使用泛型的意义。