码迷,mamicode.com
首页 > 其他好文 > 详细

泛型通配符?的使用

时间:2015-02-10 11:08:13      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

package cn.itcast.day16.generic;

import java.util.ArrayList;
import java.util.Collection;
/*
 * 泛型高级(通配符)
 * ?:
 * ? extends E:向下限定 ?代表E及其子类
 * ? super E:向上限定  ?代表E及其父类
 */
public class GenericDemo {
    public static void main(String[] args) {
        //泛型如果明确写的时候,前后必须一致
        Collection<Object> c=new ArrayList<Object>();
        Collection<Animal> c1=new ArrayList<Animal>();
//        Collection<Animal> c2=new ArrayList<Dog>();
//        Collection<Animal> c3=new ArrayList<Cat>();
        
        //? extends E:向下限定 ?代表E及其子类
//        Collection <? extends Animal> c4=new ArrayList<Object>();
        Collection <? extends Animal> c5 = new ArrayList<Animal>();
        Collection<? extends Animal> c6 = new ArrayList<Dog>();
        Collection<? extends Animal> c7 =new ArrayList<Cat>();
        
        
        // ? super E:向上限定  ?代表E及其父类
        Collection<? super Animal> c8=new ArrayList<Object>();
        Collection<? super Animal> c9=new ArrayList<Animal>();
//        Collection<? super Animal> c10=new ArrayList<Dog>();
//        Collection<? super Animal> c11=new ArrayList<Cat>();
        
        
    }

}

class Animal{
    
}
class Dog extends Animal{
}
class Cat extends Animal{
}

泛型通配符?的使用

标签:

原文地址:http://www.cnblogs.com/qq-757617012/p/4283170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!