Java-集合框架和泛型

目录

为什么需要泛型

1.泛型概述

泛型的应用

2.泛型类

3.类型通配符

4.泛型方法

类型通配符在泛型中的应用

类型通配符运用在方法上

5.泛型接口

泛型总结


为什么需要泛型

 

1.泛型概述

 参数化类型

就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也就定义成参数类型(也可以称之为参数类型),然后在使用/调用时传入具体的类型(类型参数)

泛型的应用

package Collection;

/**
 * 在类上定义泛型
 * T、E、K、V等方式作为泛型形参
 * Param<T>
 */
public class Text <T>{
    private T date;
    
    public Text(){
        
    }

    /**
     *在构造方法上使用泛型
     * @param date
     */
    public Text(T date){
        this.date=date;
    }

    /**
     * 在方法上使用泛型
     * @return data
     */
    public T getData(){
        return this.date;
    }
    
}
package Collection;

public class GenericText {

    public static void main(String[] args) {
        Text<String> name = new Text<String>("Tom");
        System.out.println("name:" + name.getDate());

        Text<Integer> num = new Text<Integer>(123);
        System.out.println("num:" + num.getDate());

    }
}

2.泛型类

package Collection;

public class GenericText {

    public static void main(String[] args) {
        Text<String> name = new Text<String>("Tom");
        System.out.println("name:" + name.getDate());

        Text<Integer> num = new Text<Integer>(123);
        System.out.println("num:" + num.getDate());

        System.out.println("name class:"+name.getClass());
        System.out.println("num class :" + num.getClass());
        System.out.println("name.getClass() == num.getClass()" + (name.getClass() == num.getClass()));//true

    }
}

检验结果正确之后,会对泛型的信息擦除 ,例如,当我们成功编译之后的class文件,它是不包含任何泛型的信息,在逻辑上泛型可以理解成多个不同的类型,但实际上都是相同的类型

3.类型通配符

在泛型中

类型通配符 ? 上线限制 extends

 类型通配符 ? 下线 super

4.泛型方法

类型通配符在泛型中的应用

package Collection;

public class GenericText {


    /**
     * 类型通配符 ? 上线限制 extends
     * @param data
     */
    public static void getUpperNumberData(Text<? extends  Number> data) {

        System.out.println("data:" + data.getData());
    }

    /**
     * 类型通配符 ? 下线 super
     * @param data
     */
    public static void getLowerNumberData(Text<? super  Number> data) {

        System.out.println("data:" + data.getData());
    }


    public static void main(String[] args) {
        Text<String > name = new Text<String>("Tom");
        Text<Number> num1 = new Text<Number>(123);//Number可以看出是父类泛型
//        System.out.println("name:" + name.getDate());

        Text<Integer> num2 = new Text<Integer>(456);
//        System.out.println("num:" + num.getDate());

        //上线测试
//        getUpperNumberData(name);
//        getUpperNumberData(num1);
//        getUpperNumberData(num2);
//
//        //下线测试
//        getLowerNumberData(name);
//        getLowerNumberData(num1);
//        getLowerNumberData(num2);

//        System.out.println("name class:"+name.getClass());
//        System.out.println("num class :" + num.getClass());
//        System.out.println("name.getClass() == num.getClass()" + (name.getClass() == num.getClass()));

    }

}

类型通配符运用在方法上

    /**
     * 将泛型定义在方法上
     *泛型必须放到返回值的前面 修饰符的后面
     * @param e
     * @param <E>
     */
    public <E> void setData(E e){
        System.out.println(e.toString());
    }
  Text test = new Text();
  test.setData("Jerry");

5.泛型接口

接口:

package Collection.serivce;

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

}

接口实现:

package Collection.serivce.impl;

import Collection.serivce.Inter;

public class Interimpl01 implements Inter<String> {

    public  void show(String str){

        System.out.println("show" + str);
    }
}
package Collection.serivce.impl;

import Collection.serivce.Inter;

public class Interimpl02<W> implements Inter<W> {

    public void show(W q){
        System.out.println("show" + q);
    }
}

类中测试:

        public static void main(String[] args) {
        Interimpl01 impl01 = new Interimpl01();
        impl01.show("abc");
        Interimpl02<Integer> impl02 = new Interimpl02<Integer>();
        impl02.show(5);

        System.out.println("--------------");

泛型总结

 1.泛型提高了程序的安全性

2.将运行期间遇到的问题转移到编译期

3.省去了类型强制转换的麻烦

4.泛型类的出现优化了程序设计

5.在接口、类、方法上都可以使用泛型

6.类型通配符上、下限的定义

<? extends E> 、<? super E>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

什么时候养猫猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值