Java集合学习:泛型

Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。

常见形式有 泛型类、泛型接口、泛型方法。

语法: T称为类型占位符, 表示一种引用类型(也只能时引用类型)。是一种不确定的引用类型,由程序员定义。
好处:
(1) 提高代码的重用性
(2)防止类型转换异常,提高代码的安全性

先看泛型类:

直接看代码:

package com.collections.generic;
/*
泛型类
T是类型占位符,表示一种引用类型,如果编写多个就用逗号隔开
*/
public class MyGeneric {
//    1.创建变量
    T t;
//    2.定义一个方法
    public void show(T t){
        System.out.println(t);
    }
//    3.泛值作为方法的返回值
    public T getT(){
        return t;
    }
}

这个就是一个最基本的泛型类。

因为还不确定时何种引用类型,所以可以创建变量而不能new。

在主方法中的调用:分别定义了String类型和Integer类型的泛型类

package com.collections.generic;

public class TestGeneric {
//    1.泛型只能使用对应的引用类型 2.不同泛型对象之间不能相互赋值
    public static void main(String[] args) {
//        调用泛型类,在这里定义泛型是什么引用类型
        MyGeneric myGeneric = new MyGeneric();
        myGeneric.t = "Hello World!";
        myGeneric.show("你好");
        String t = myGeneric.getT();
        System.out.println(t);
        System.out.println("---------------------------");
        MyGeneric myGeneric2 = new MyGeneric<>();
        myGeneric2.t = 12345;
        Integer integer = myGeneric2.getT();
        System.out.println(integer);
        myGeneric2.show(123)
}

运行结果:   

      

泛型接口:

package com.collections.generic;
/*
泛型接口
注意:不能创建泛型静态常量
 */
public interface MyInterface {
    String name = "张三";

    T server(T t);
}

泛型接口有两种实现方法:

第一种:在实现接口的时候,就确定泛型的类型了

package com.collections.generic;
//泛型接口实现类
//第一种实现类:在实现接口的时候,就确定泛型的类型了
public class MyInterfaceImpl  implements MyInterface{
    @Override
    public String server(String s) {
        System.out.println(s);
        return s;
    }
}

第二种:在调用泛型接口的实现方法时在定义泛型,两个都是引用类型T

package com.collections.generic;
//第二种实现类: 两个都是引用类型T
public class MyInterfaceImpl2 implements MyInterface {
    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}

来看一下两种泛型接口实现方法的调用:

主方法:

package com.collections.generic;

public class TestGeneric {
//    1.泛型只能使用对应的引用类型 2.不同泛型对象之间不能相互赋值
    public static void main(String[] args) {
        System.out.println("-------------泛型接口1----------------");
        MyInterfaceImpl myInterface = new MyInterfaceImpl();
        myInterface.server("aaa");
        System.out.println("-------------泛型接口2----------------");
//        第二种泛型接口在这里确定类型
        MyInterfaceImpl2 myInterfaceImpl2  = new MyInterfaceImpl2<>();
        myInterfaceImpl2.server(123);
        System.out.println("-------------------泛型方法-----------");
        MyGenericMethod myGenericMethod = new MyGenericMethod();
    }
}

运行结果:

 Java集合学习:泛型_第1张图片

 

泛型方法:

package com.collections.generic;

/**
 * 泛型方法
 */
public class MyGenericMethod {
    public  T show(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}

最简单的泛型方法。

泛型方法的调用:

package com.collections.generic;

public class TestGeneric {
//    1.泛型只能使用对应的引用类型 2.不同泛型对象之间不能相互赋值
    public static void main(String[] args) {
//        方法中你传什么类型的参数就是什么类型的方法
//        实现了自动装箱的操作
        myGenericMethod.show("华子");
        myGenericMethod.show(123);
        myGenericMethod.show(3.14);
    }
}

运行结果:

Java集合学习:泛型_第2张图片 

附上完整主方法:

package com.collections.generic;

public class TestGeneric {
    //    1.泛型只能使用对应的引用类型 2.不同泛型对象之间不能相互赋值
    public static void main(String[] args) {
//        调用泛型类,在这里定义泛型是什么引用类型
        MyGeneric myGeneric = new MyGeneric();
        myGeneric.t = "Hello World!";
        myGeneric.show("你好");
        String t = myGeneric.getT();
        System.out.println(t);
        System.out.println("---------------------------");
        MyGeneric myGeneric2 = new MyGeneric<>();
        myGeneric2.t = 12345;
        Integer integer = myGeneric2.getT();
        System.out.println(integer);
        myGeneric2.show(123);
        System.out.println("-------------泛型接口1----------------");
        MyInterfaceImpl myInterface = new MyInterfaceImpl();
        myInterface.server("aaa");
        System.out.println("-------------泛型接口2----------------");
//        第二种泛型接口在这里确定类型
        MyInterfaceImpl2 myInterfaceImpl2  = new MyInterfaceImpl2<>();
        myInterfaceImpl2.server(123);
        System.out.println("-------------------泛型方法-----------");
        MyGenericMethod myGenericMethod = new MyGenericMethod();
//        方法中你传什么类型的参数就是什么类型的方法
//        实现了自动装箱的操作
        myGenericMethod.show("华子");
        myGenericMethod.show(123);
        myGenericMethod.show(3.14);
    }
}

最后看下泛型集合:

特点: 

编译时即可检查,而非运行时抛出异常。

访问时,不必类型转换(拆箱)。

不同泛型之间引用不能相互赋值,泛型不存在多态。

看代码:

package com.collections.test;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 泛型集合
 */
public class Demo {
    public static void main(String[] args) {
//        避免使用元素时进行非必要的强制类型转换\
        ArrayList arrayList = new ArrayList();
        arrayList.add("aaa");
        arrayList.add("bbb");
        arrayList.add(10);
        arrayList.add(20);
        for (Object obj:arrayList) {
            System.out.println(obj);
        }
//     未加泛型时,集合中由不同元素时,遍历集合只能时Object的类型
        ArrayList arrayList1 = new ArrayList<>();
        Student s1 = new Student("aaa",12);
        Student s2 = new Student("bbb",12);
        Student s3 = new Student("ccc",12);
//        添加泛型时,类型就固定了,不会出现强制类型转换失败
        System.out.println("--------------------------------");
        arrayList1.add(s1);
        arrayList1.add(s2);
        arrayList1.add(s3);
        Iterator iterator = arrayList1.iterator();
        while (iterator.hasNext()){
            Student s = iterator.next();
            System.out.println(s);
        }
    }
}

主要就是防止集合中引用类型的不同而引起的报错。


 

 

 

 

 

 

 


 

你可能感兴趣的:(Java学习进阶,java,学习,开发语言)