泛型

3.1 泛型概述

在前面学习集合时,我们都知道集合中是可以存放任意对象的,只要把对象存储集合后,那么这时他们都会被提升成 Object 类型。当我们在取出每一个对象,并且进行相应的操作,这时必须采用类型转换。

大家观察下面代码:

  1. public class GenericDemo {
  2. public static void main(String[] args) {
  3. Collection coll = new ArrayList();
  4. coll.add("abc");
  5. coll.add("itcast");
  6. coll.add(5);//由于集合没有做任何限定,任何类型都可以给其中存放
  7. Iterator it = coll.iterator();
  8. while(it.hasNext()){
  9. //需要打印每个字符串的长度,就要把迭代出来的对象转成String类型
  10. String str = (String) it.next();
  11. System.out.println(str.length());
  12. }
  13. }
  14. }

程序在运行时发生了问题java.lang.ClassCastException。 为什么会发生类型转换异常呢? 我们来分析下:由于集合中什么类型的元素都可以存储。导致取出时强转引发运行时 ClassCastException。 怎么来解决这个问题呢? Collection 虽然可以存储各种对象,但实际上通常 Collection 只存储同一类型对象。例如都是存储字符串对象。因此在 JDK5 之后,新增了泛型(Generic)语法,让你在设计 API 时可以指定类或方法支持泛型,这样我们使用 API 的时候也变得更为简洁,并得到了编译时期的语法检查。

  • 泛型:可以在类或方法中预支地使用未知的类型。

tips:一般在创建对象时,将未知的类型确定具体的类型。当没有指定泛型时,默认类型为 Object 类型。

3.2 使用泛型的好处

上一节只是讲解了泛型的引入,那么泛型带来了哪些好处呢?

  • 将运行时期的 ClassCastException,转移到了编译时期变成了编译失败。
  • 避免了类型强转的麻烦。

通过我们如下代码体验一下:

  1. public class GenericDemo2 {
  2. public static void main(String[] args) {
  3. Collection<String> list = new ArrayList<String>();
  4. list.add("abc");
  5. list.add("itcast");
  6. // list.add(5);//当集合明确类型后,存放类型不一致就会编译报错
  7. // 集合已经明确具体存放的元素类型,那么在使用迭代器的时候,迭代器也同样会知道具体遍历元素类型
  8. Iterator<String> it = list.iterator();
  9. while(it.hasNext()){
  10. String str = it.next();
  11. //当使用Iterator<String>控制元素类型后,就不需要强转了。获取到的元素直接就是String类型
  12. System.out.println(str.length());
  13. }
  14. }
  15. }

tips:泛型是数据类型的一部分,我们将类名与泛型合并一起看做数据类型。

3.3 泛型的定义与使用

我们在集合中会大量使用到泛型,这里来完整地学习泛型知识。

泛型,用来灵活地将数据类型应用到不同的类、方法、接口当中。将数据类型作为参数进行传递。

定义和使用含有泛型的类

定义格式:

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

例如,API 中的 ArrayList 集合:

  1. class ArrayList<E>{
  2. public boolean add(E e){ }
  3. public E get(int index){ }
  4. ....
  5. }

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

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

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

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

  1. class ArrayList<String>{
  2. public boolean add(String e){ }
  3. public String get(int index){ }
  4. ...
  5. }

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

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

  1. class ArrayList<Integer> {
  2. public boolean add(Integer e) { }
  3. public Integer get(int index) { }
  4. ...
  5. }

举例自定义泛型类

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

使用:

  1. public class GenericClassDemo {
  2. public static void main(String[] args) {
  3. // 创建一个泛型为String的类
  4. MyGenericClass<String> my = new MyGenericClass<String>();
  5. // 调用setMVP
  6. my.setMVP("大胡子登登");
  7. // 调用getMVP
  8. String mvp = my.getMVP();
  9. System.out.println(mvp);
  10. //创建一个泛型为Integer的类
  11. MyGenericClass<Integer> my2 = new MyGenericClass<Integer>();
  12. my2.setMVP(123);
  13. Integer mvp2 = my2.getMVP();
  14. }
  15. }

含有泛型的方法

定义格式:

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

例如,

  1. public class MyGenericMethod {
  2. public <MVP> void show(MVP mvp) {
  3. System.out.println(mvp.getClass());
  4. }
  5. public <MVP> MVP show2(MVP mvp) {
  6. return mvp;
  7. }
  8. }

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

  1. public class GenericMethodDemo {
  2. public static void main(String[] args) {
  3. // 创建对象
  4. MyGenericMethod mm = new MyGenericMethod();
  5. // 演示看方法提示
  6. mm.show("aaa");
  7. mm.show(123);
  8. mm.show(12.45);
  9. }
  10. }

含有泛型的接口

定义格式:

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

例如,

  1. public interface MyGenericInterface<E>{
  2. public abstract void add(E e);
  3. public abstract E getE();
  4. }

使用格式:

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

例如

  1. public class MyImp1 implements MyGenericInterface<String> {
  2. @Override
  3. public void add(String e) {
  4. // 省略...
  5. }
  6. @Override
  7. public String getE() {
  8. return null;
  9. }
  10. }

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

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

例如

  1. public class MyImp2<E> implements MyGenericInterface<E> {
  2. @Override
  3. public void add(E e) {
  4. // 省略...
  5. }
  6. @Override
  7. public E getE() {
  8. return null;
  9. }
  10. }

确定泛型:

  1. /*
  2. * 使用
  3. */
  4. public class GenericInterface {
  5. public static void main(String[] args) {
  6. MyImp2<String> my = new MyImp2<String>();
  7. my.add("aa");
  8. }
  9. }

3.4 泛型通配符

当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用 Object 类中的共性方法,集合中元素自身方法无法使用。

通配符基本使用

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

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

举个例子大家理解使用即可:

  1. public static void main(String[] args) {
  2. Collection<Intger> list1 = new ArrayList<Integer>();
  3. getElement(list1);
  4. Collection<String> list2 = new ArrayList<String>();
  5. getElement(list2);
  6. }
  7. public static void getElement(Collection<?> coll){}
  8. //?代表可以接收任意类型

tips:泛型不存在继承关系 Collection

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

之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在 JAVA 的泛型中可以指定一个泛型的上限下限

泛型的上限

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

泛型的下限

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

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

  1. public static void main(String[] args) {
  2. Collection<Integer> list1 = new ArrayList<Integer>();
  3. Collection<String> list2 = new ArrayList<String>();
  4. Collection<Number> list3 = new ArrayList<Number>();
  5. Collection<Object> list4 = new ArrayList<Object>();
  6. getElement(list1);
  7. getElement(list2);//报错
  8. getElement(list3);
  9. getElement(list4);//报错
  10. getElement2(list1);//报错
  11. getElement2(list2);//报错
  12. getElement2(list3);
  13. getElement2(list4);
  14. }
  15. // 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
  16. public static void getElement1(Collection<? extends Number> coll){}
  17. // 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
  18. public static void getElement2(Collection<? super Number> coll){}