java泛型

泛型

泛型是一种未知的数据类型,当我们不知道使用什么数据类型的时候,可以使用泛型

泛型也可以看成是一个变量,用来接收数据类型

E e :Element 元素

T t Type 类型

ArrayList集合在定义的时候,不知道集合都会存储什么类型的数据,所以类型使用泛型

E :未知的数据类型

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

当创建集合对象的时候,就会确定泛型的数据类型

ArrayList<String> list = new ArrayList<String>();会把数据类型String作为参数传递给E

1.使用泛型的好处

package package20Foreach.Demo01;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 20:18 2021/10/7
 * @Modified By:
 */
public class Demo02Generic {
    public static void main(String[] args) {
        show01();
        show02();
    }

    /*
    * 创建集合对象,使用泛型
    * 好处:
    *   1.避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型
    *   2.把运行期异常(代码运行之后抛出的异常),提升到编译器(写代码的时候就会报错)
    * 弊端:
    *   泛型是什么类型,就只能存储什么类型的数据
    * */
    private static void show02() {
        ArrayList<String> list = new ArrayList<>();
        list.add("abc");
//        list.add(1);//报错

        //使用迭代器遍历list集合;
        Iterator<String> it = list.iterator();
        while (it.hasNext()){
            String s = it.next();
            System.out.println(s+"->"+s.length());

        }
    }

    /*
     * 创建集合对象,不使用泛型
     * 好处:集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据
     * 弊端:不安全,引发异常
     * */
    private static void show01() {
        ArrayList list = new ArrayList();
        list.add("abs");
        list.add(1);

        //使用迭代器遍历集合
        //获取迭代器
        Iterator it = list.iterator();
        //使用迭代器中的方法hasNext和next遍历集合
        while (it.hasNext()){
            Object obj = it.next();
            System.out.println(obj);

            //想要使用String类特有的方法,length获取字符串的长度;不能使用    因为使用的是多态 Object obj = "abc"
            //需要向下转型
            //ClassCastException 会抛出类型转换异常,不能把Integer类型,转换成String类型
//            String s = (String)obj;
//            System.out.println(s.length());


        }
    }
}

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

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 20:43 2021/10/7
 * @Modified By:
 */

/*
* 定义一个含有泛型的类,模拟ArrayList集合
* 泛型是一个未知的数据类型,当我们不确定什么什么数据类型的时候,可以使用泛型
* 泛型可以接收任意的数据类型,可以使用Integer、String、Student。。。
* 创建对象的时候确定泛型的数据类型
* */
public class GenericClass<E> {
    private E name;

    public E getName() {
        return name;
    }

    public void setName(E name) {
        this.name = name;
    }
}

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 20:47 2021/10/7
 * @Modified By:
 */
public class Demo01GenericClass {
    public static void main(String[] args) {
        //不写泛型默认为Object类型
        GenericClass gc = new GenericClass();
        gc.setName("只能是字符串");
        Object name = gc.getName();
        System.out.println(name);

        //创建GenericClass对象,泛型使用Integer类型
        GenericClass<Integer> gc2 = new GenericClass<>();
        gc2.setName(1);

        Integer name1 = gc2.getName();
        System.out.println(name1);

        //创建GenericClass对象,泛型使用String 类型
        GenericClass<String> gc3 = new GenericClass<>();
        gc3.setName("小明");

        String name2 = gc3.getName();
        System.out.println(name2);
    }
}

3.含有泛型的方法

格式:

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

含有泛型的方法,在调用方法的时候确定泛型的数据类型

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 21:30 2021/10/7
 * @Modified By:
 */
/*
* 定义含有泛型的方法:泛型定义在方法的修饰符和返回值之间;
* 含有泛型的方法,在调用方法的时候确定泛型的数据类型
* 传递什么类型的参数,泛型就是什么类型
* */
public class GenericMethod {
    //定义一个含有泛型的方法
    public<M> void method01(M m){
        System.out.println(m);
    }

    //定义一个含有泛型的静态方法
    public static<S> void method02(S s){
        System.out.println(s);
    }
}

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 21:34 2021/10/7
 * @Modified By:
 */

/*
* 测试含有泛型的方法
* */
public class Demo02GenericMethod {
    public static void main(String[] args) {
        //创建GenericMethod对象
        GenericMethod gm = new GenericMethod();

        /*
         * 调用含有泛型的方法method01()
         * 传递什么类型,泛型就是什么类型
         * */
        gm.method01(10);
        gm.method01("abc");
        gm.method01(8.8);
        gm.method01(true);

        gm.method02("静态方法不建议创建对象使用");

        //静态方法,通过类名.方法名(参数),可以直接使用
        GenericMethod.method02("abc");
        GenericMethod.method02(1);
    }
}

4.定义含有泛型的接口

格式:

修饰符 interface 方法名<代表泛型的变量(I)> {
    public abstract void method(I i(使用泛型));
}
package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 21:52 2021/10/7
 * @Modified By:
 */
public interface GenericInterface<I> {
    public abstract void method(I i);
}

含有泛型的接口,第一种使用方法:定义接口的实现类,实现接口,指定接口的泛型

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 21:53 2021/10/7
 * @Modified By:
 */

/*
* 含有泛型的接口,第一种使用方法:定义接口的实现类,实现接口,指定接口的泛型
*
* */
public class GenericInterfaceImpl1 implements GenericInterface<String>{
    @Override
    public void method(String s) {
        System.out.println(s);
    }
}

含有泛型的接口,第二种使用方法,:接口使用什么类型,类就跟着接口走,就相当于定义了一个含有泛型的类

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 22:00 2021/10/7
 * @Modified By:
 */

/*
* 含有泛型的接口,第二种使用方法,:接口使用什么类型,类就跟着接口走
* 就相当于定义了一个含有泛型的类
* */
public class GenericInterfaceImpl2<I> implements GenericInterface<I> {
    @Override
    public void method(I i) {
        System.out.println(i);
    }
}

实例调用

package package21FanXing.Demo02;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 21:58 2021/10/7
 * @Modified By:
 */

/*
* 测试含有泛型的接口
* */
public class Demo03GenericInterface {
    public static void main(String[] args) {
        //创建GenericInterfaceImpl1对象
        GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl1();
        gi1.method("字符串");

        //创建GenericInterfaceImpl2对象
        GenericInterfaceImpl2<Integer> gi2 = new GenericInterfaceImpl2<>();
        gi2.method(10);

        GenericInterfaceImpl2<Double> gi3 = new GenericInterfaceImpl2<>();
        gi3.method(9.9);
    }
}

5.泛型通配符

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

通配符的基本使用

泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示 未知通配符 此时只能接收数据,不能往该集合中存储数据

package package21FanXing.Demo02;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 22:25 2021/10/7
 * @Modified By:
 */

/*
* 泛型通配符
*   ?: 代表任意的数据
* 使用方式:
*   不能创建对象使用
*   只能作为方法的参数使用
* */
public class Demo04Generic {
    public static void main(String[] args) {
        ArrayList<Integer> list01 = new ArrayList<>();
        list01.add(1);
        list01.add(2);

        ArrayList<String> list02 = new ArrayList<>();
        list02.add("a");
        list02.add("b");

        printArray(list01);
        printArray(list02);
    }

    /*
    * 定义一个方法,能遍历所有的ArrayList集合
    * 这时候我们不知道ArrayList集合使用什么数据类型,可以使用泛型的通配符“?”来接收数据类型
    * */
    public static void printArray(ArrayList<?> list){
        //使用迭代器遍历集合
        Iterator<?> it = list.iterator();
        while (it.hasNext()) {
            //it.next()方法,取出的元素是object,因为只有Object类型才能接收任意的数据类型
            Object next = it.next();
            System.out.println(next);
        }
    }
}

6.泛型通配符的高级使用

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

泛型的上限:

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

泛型的下限:

  • 格式:类型名称<? super 类> 对象名称
  • 意义:只能接收该类型及其父类型
package package21FanXing.Demo02;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @Author:Fwh
 * @Description:
 * @Date:Creat in 22:41 2021/10/7
 * @Modified By:
 */

/*
* 泛型的上限:? extends E 代表使用的泛型只能是E类型的子类/本身
* 泛型的下限:? super E 代表使用的泛型是能是E类型的父类/本身
*
* */
public class Demo05Generic {
    public static void main(String[] args) {
        Collection<Integer> lsit1 = new ArrayList<>();
        Collection<String> lsit2 = new ArrayList<>();
        Collection<Number> lsit3 = new ArrayList<>();
        Collection<Object> lsit4 = new ArrayList<>();

        getElment1(lsit1);
//        getElment1(lsit2);//报错,只能访问Number的子类和Number本身
        getElment1(lsit3);
//        getElment1(lsit4);

//        getElment2(lsit1);//报错,只能访问Number的父类和Number本身
//        getElment2(lsit2);
        getElment2(lsit3);
        getElment2(lsit4);


        /*
        * 类与类之间的继承关系
        * Integer extends Number extends Object
        * String extends Object
        * */
    }

    /*
    * 泛型的下限:? super E 代表使用的泛型是能是E类型的父类/本身
    * */
    private static void getElment2(Collection<?super Number> coll ) {
    }

    /*
    * 泛型的上限:? extends E 代表使用的泛型只能是E类型的子类/本身
    * */
    private static void getElment1(Collection<?extends Number> coll) {
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值