泛型总结

泛型类定义的泛型,在整个类中有效。
如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。

为了让不同的方法可以限定不同类型。那么 可以将泛型定义在方法上。

泛型类

class Demo<T>
{
    public void show(T t) {
        System.out.println("show: "+t);
    }
    public void print(T t) {
        System.out.println("show: "+t);
    }
}

class GenericDemo4 {
    public static void main(String[] args) {
        Demo<Integer>d = new Demo<Integer>();
        d.show(new Integer(4));
        Demo<String>d1 = new Demo<String>();
        d1.print("haha");
    }
}

结果:
show: 4
show: haha

泛型方法

class Demo{
    public <T> void show(T t){
        System.out.println("show: "+t);
    }
    public <Q> void print(Q q)
    {
        System.out.println("print:"+q);
    }
}

class GenericDemo4
{
    public static void main(String[] args)
    {
        Demo d = new Demo();
        d.show("hello boy!");
        d.print("Alex i love you !");
    }
}

结果:
show: hello boy!
print:Alex i love you !

同时定义泛型类和泛型方法

class Demo<T>
{
    public void show(T t)
    {
        System.out.println("show: "+t);
    }
    public <Q> void print(Q q)
    {
        System.out.println("print:"+q);
    }
}
class GenericDemo4
{
    public static void main(String[] args)
    {
        Demo <String> d = new Demo<String>();
        d.show("hello boy!");
        d.print("Alex i love you !");
        d.print(5);
        d.print("heiei");

    }
}

结果:
show: hello boy!
print:Alex i love you !
print:5
print:heiei

特殊之处:
静态方法不可以访问类上定义的泛型
如果静态方法操作的应用数据类型不确定或者它和类上定义的泛型不同,可以将泛型定义在方法上。

class Demo<T> {
    public void show(T t) {
        System.out.println("show: "+t);
    }
    public <Q> void print(Q q)  {
        System.out.println("print:"+q);
    }

    public static <W>void method(W t) {
        System.out.println("method: "+t);
    }
}


class GenericDemo4{
    public static void main(String[] args) {
        Demo <String> d = new Demo<String>();
        d.show("hello boy!");
        d.print("Alex i love you !");

        d.print(5);
        d.print("heiei");
        Demo.method("hihi");
    }
}

结果:
show: hello boy!
print:Alex i love you !
print:5
print:heiei
method: hihi

泛型定义在接口上

interface Inter<T> {
    void show(T t);
}

//第一种
class InterImpl implements Inter<String> {   
    public void show(String t) {
        System.out.println("show :"+t);
    }
}

/*第二种
class InterImpl<T>implements Inter<T> {
    public void show(T t) {
        System.out.println("show :"+t);
    }
}
*/
class GenericDemo5
{
    public static void main(String[] args)
    {
        /*  
        InterImpl<Integer> i = new InterImpl<Integer>();
        i.show(4);
        */
        InterImpl i = new InterImpl();
        i.show("haha");

    }
}

结果:
show :haha

第一种相对来说就比较死,固定为String类型了。而第二种可以自己定义。
下面分享两篇讲泛型的文章,这两篇文章讲的深入浅出,基本能覆盖的也都覆盖了。感谢原作者!!

java泛型(二)、泛型的内部原理:类型擦除以及类型擦除带来的问题

Java中的逆变与协变

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、泛型简介1.引入泛型的目的 了解引入泛型的动机,就先从语法糖开始了解。 语法糖 语法糖(Syntactic S...
    Android进阶与总结阅读 1,033评论 0 9
  • 一、基本概念和用法 在Java语言处于还没有出现泛型的版本时,只能通过Object是所有类型的父类和类型强制转换两...
    developerzjy阅读 4,469评论 3 47
  • Java泛型总结# 泛型是什么## 从本质上讲,泛型就是参数化类型。泛型十分重要,使用该特性可以创建类、接口以及方...
    kylinxiang阅读 936评论 0 1
  • Java泛型总结 泛型使用 从Java5开始引入了“参数化类型”的概念,允许在创建集合的时候指定集合元素的类型 J...
    Cool_Pomelo阅读 114评论 0 0
  • Java泛型总结 Table of Contents 一.概述1.什么是Java泛型2.Java泛型的意义 二.泛...
    fanshanhong阅读 291评论 0 0