虾屮蛋

导航

 

java1.5中开始引入泛型,平时用的也挺多,下面例子大概总结下基本用法:

/**
 * 泛型测试类
 */
public class GenericDemo<T, E, H extends Collection> {
    
    private T obj1;
    private E obj2;
    
    public GenericDemo () {
        
    }
    
    /**
     * List参数必须继承String类,否则编译报错
     */
    public void method1(List<? extends String> list) {
        
    }

    /**
     * List参数必须是String的父类
     */
    public void method2(List<? super String> list) {
        
    }
    
    public List<? extends Exception> method3() {
        return null;
    }
    
    public Map<T, E> method4() {
        return null;
    }
    
    public T method5() {
        return null;
    }
    
    public static void main(String []args) {
        // 实例化变量时,指定具体的泛型类
        new GenericDemo<String, String, List<String>>();
    }
    

 

 

posted on 2013-04-01 11:01  虾屮蛋  阅读(115)  评论(0)    收藏  举报