Java泛型

一、泛型概述

​ 在定义类或方法时,可以先不确定具体的类型,一般可以以用E来表示,到了创建对象时,再将未知的类型E确定为具体的类型。可以理解为将数据类型作为参数进行传递。

​ 在创建对象时确定具体的类型可以避免出现运行时的ClassCastException异常,因为这样的问题在编译期就可以被编译器发现,编译报错。

二、泛型的定义和使用

​ 我们会在集合中大量使用泛型。

1. 定义和使用含有泛型的类

定义格式:

修饰符 class 类名<代表泛型的变量> {  }

例如,API中的ArrayList集合:

class ArrayList<E>{ 
    public boolean add(E e){ }

    public E get(int index){ }
   	....
}

使用泛型: 即什么时候确定泛型?

在创建对象的时候确定泛型

例如,ArrayList<String> list = new ArrayList<>();

此时,变量E的值就是String类型,那么我们的类型就可以理解为:

class ArrayList<String>{ 
     public boolean add(String e){ }

     public String get(int index){  }
     ...
}

再例如,ArrayList<Integer> list = new ArrayList<>();

此时,变量E的值就是Integer类型,那么我们的类型就可以理解为:

class ArrayList<Integer> { 
     public boolean add(Integer e) { }

     public Integer get(int index) {  }
     ...
}

自定义泛型类举例

public class MyGenericClass<MVP> {
	//没有MVP类型,在这里代表未知的一种数据类型,未来传递什么就是什么类型
	private MVP mvp;
     
    public void setMVP(MVP mvp) {
        this.mvp = mvp;
    }
     
    public MVP getMVP() {
        return mvp;
    }
}

使用:

public class GenericClassDemo {
  	public static void main(String[] args) {		 
         // 创建一个泛型为String的类
         MyGenericClass<String> my = new MyGenericClass<>();    	
         // 调用setMVP
         my.setMVP("大胡子登登");
         // 调用getMVP
         String mvp = my.getMVP();
         System.out.println(mvp);
         //创建一个泛型为Integer的类
         MyGenericClass<Integer> my2 = new MyGenericClass<>(); 
         my2.setMVP(123);   	  
         Integer mvp2 = my2.getMVP();
         System.out.println(mvp2);
    }
}

2. 定义和使用含有泛型的方法

定义格式:

修饰符 <代表泛型的变量> 返回值类型 方法名(参数){  }

例如,

public class MyGenericMethod {	  
    public <MVP> void show(MVP mvp) {
    	System.out.println(mvp.getClass());
    }
    
    public <MVP> MVP show2(MVP mvp) {	
    	return mvp;
    }
}

使用格式:调用方法时,确定泛型的类型

public class GenericMethodDemo {
    public static void main(String[] args) {
        // 创建对象
        MyGenericMethod mm = new MyGenericMethod();
        // 调用方法时,确定泛型的类型
        mm.show("aaa");// 输出:class java.lang.String
        mm.show(123);  // 输出:class java.lang.Integer
        mm.show(12.45);// 输出: class java.lang.Double
        
        String s = mm.show2("bbb");
        System.out.println(s);// 输出: bbb
    }
}

3. 定义和使用含有泛型的接口

定义格式:

修饰符 interface 接口名<代表泛型的变量> {  }

例如,

public interface MyGenericInterface<E>{
	public abstract void add(E e);
	
	public abstract E getE();  
}

使用格式:

1、定义类时确定泛型的类型

例如

public class MyImp1 implements MyGenericInterface<String> {
	@Override
    public void add(String e) {
        // 省略...
    }

	@Override
	public String getE() {
		// 省略...
	}
}

此时,泛型E的值就是String类型。

2、始终不确定泛型的类型,直到创建对象时,确定泛型的类型

例如

public class MyImp2<E> implements MyGenericInterface<E> {
	@Override
	public void add(E e) {
       	 // 省略...
	}

	@Override
	public E getE() {
		// 省略...
	}
}

确定泛型:

public class GenericInterface {
    public static void main(String[] args) {
        MyImp2<String>  my = new MyImp2<>();  
        my.add("aa");
    }
}

三、泛型通配符

​ 当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用Object类中的方法,集合中元素自身的方法无法使用(因为会把原有的数据类型提升为Object类型)。

1. 通配符的基本使用

泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。

此时只能接受数据,不能往该集合中存储数据。

举例方便理解:

public static void main(String[] args) {
    Collection<Intger> list1 = new ArrayList<Integer>();
    getElement(list1);
    Collection<String> list2 = new ArrayList<String>();
    getElement(list2);
}
//?代表可以接收任意类型
public static void getElement(Collection<?> coll){
    // 省略...
}

【注】泛型不存在继承关系 Collection<Object> list = new ArrayList<String>();这种写法是错误的。

2. 通配符的高级使用——受限泛型

​ 上面的?是可以接收任意类型的。但是在Java的泛型中你也可以指定接收类型的上限下限

泛型的上限

  • 格式类型名称 <? extends 类 > 对象名称
  • 意义只能接收该类型及其子类

泛型的下限

  • 格式类型名称 <? super 类 > 对象名称
  • 意义只能接收该类型及其父类型

比如:现已知Object类,String 类,Number类,Integer类,其中Number是Integer的父类

public static void main(String[] args) {
    Collection<Integer> list1 = new ArrayList<Integer>();
    Collection<String> list2 = new ArrayList<String>();
    Collection<Number> list3 = new ArrayList<Number>();
    Collection<Object> list4 = new ArrayList<Object>();
    
    getElement(list1);
    getElement(list2);//报错
    getElement(list3);
    getElement(list4);//报错
  
    getElement2(list1);//报错
    getElement2(list2);//报错
    getElement2(list3);
    getElement2(list4);
  
}
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement1(Collection<? extends Number> coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement2(Collection<? super Number> coll){}

原文地址:https://www.cnblogs.com/jiajun107/p/12988112.html