泛型Generic

泛型:可以在类或方法中预支地使用未知的类型。相当于标签,作用于容器。

使用泛型的好处:
* 将运行时期的ClassCastException,转移到了编译时期变成了编译失败。*
* 避免了类型强转的麻烦。*

        Collection<String> list = new ArrayList<String>();
        list.add("abc");
        list.add("itcast");
        // list.add(5);//当集合明确类型后,存放类型不一致就会编译报错
        // 集合已经明确具体存放的元素类型,那么在使用迭代器的时候,迭代器也同样会知道具体遍历元素类型
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String str = it.next();
            //当使用Iterator<String>控制元素类型后,就不需要强转了。获取到的元素直接就是String类型
            System.out.println(str.length());
        }

tips:
1.一般在创建对象时,将未知的类型确定具体的类型。当没有指定泛型时,默认类型为Object类型。
2.泛型是数据类型的一部分,我们将类名与泛型合并一起看做数据类型。

泛型的定义与使用

修饰符 class 类名<代表泛型的变量> { }
// **泛型在定义的时候不具体,使用的时候才变得具体。在使用的时候确定泛型的具体数据类型.
class ArrayList<E>{
    public boolean add(E e){ }

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

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

// 创建对象时
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<Integer>();

含有泛型的方法:

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

class MyGeneric{
    public <T> void show(T t){
        sout(t.getClass());
    }
}

// 调用方法时,确定泛型的类型

    public static void main(String[] args) {

        MyGeneric m = new MyGeneric();
        m.show("123"); // class java.lang.String
        m.show(new Object());// class java.lang.Object

    }

含有泛型的接口:

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

interface MyGenericInterface<E>{
    void add(E e);

    E getE();
}

// 定义类时确定泛型的类型
class MyImp1 implements MyGenericInterface<String> {
    @Override
    public void add(String e) {
        // ...
    }

    @Override
    public String getE() {
        return null;
    }
}

// 始终不确定泛型的类型,直到创建对象时,确定泛型的类型
class MyImp2<E> implements MyGenericInterface<E> {
    @Override
    public void add(E e) {
        // ...
    }

    @Override
    public E getE() {
        return null;
    }
}

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

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

通配符基本使用

泛型的通配符:**不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。**
此时只能接受数据,不能往该集合中存储数据。
    PSVM(){
        Collection<Integer> 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>();这种是错误的

通配符高级使用
之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在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){}
全部评论

相关推荐

牛客96763241...:杭电✌️也是打完招呼,没人回吗
点赞 评论 收藏
分享
09-18 20:41
门头沟学院 Java
要个offer怎么这...:哈哈哈哈哈哈,我也拿了0x10000000个offer,秋招温啦啦啦,好开心
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务