泛型定义在接口上

/*泛型定义在接口上
 *
 * */

package 泛型;


interface Inter<T>{
    void show(T t);
}
//第一种方式
class InterImpl implements Inter<String>{

    public void show(String t) {
        System.out.println("show:"+t);
    }
    
}
//第二种方式
class InterImpl2<T> implements Inter<T>{

    public void show(T t) {
        System.out.println("show:"+t);
        
    }
    
}

public class GenericDemo2 {
    public static void main(String[] args) {
        InterImpl i = new InterImpl();
        i.show("haha");
        
        InterImpl2<Integer> i2 = new InterImpl2<Integer>();
        i2.show(4);
    }
}

posted @ 2016-05-01 22:43  宫学良  阅读(442)  评论(0编辑  收藏  举报