泛型

泛型

  • 1、 : T 占位符 表示 当前类是 泛型类
  • 2、放数据时 可以自动进行类型检查
  • 3、取数据时 可以自动进行类型转换
  • 4、泛型是在编译时期的一种机制 -》擦除机制 。 编译时 按照 Object 编译,不是替换成 Object
  • 5、泛型是有边界的
  • 不能 new 泛型类型的数组
  • 泛型类型的参数 不参与类型组成
  • 简单类型 不能 作为泛型类型的参数
class MyStack<T> {
    public T[] elem;
    public int top;

    public MyStack() {
        //this.elem = new T[10];//error 不能 new 泛型类型的数组
        this.elem = (T[]) new Object[10];
    }

    public void push(T val) {
        this.elem[this.top] = val;
        this.top++;
    }

    public T peek() {
        return this.elem[top-1];
    }
}

public class TestDemo2 {
    public static void main(String[] args) {
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(10);
        myStack.push(20);

        int ret = myStack.peek();
        System.out.println(ret);//20

        MyStack<String> myStack2 = new MyStack<>();
        myStack2.push("abcd");
        myStack2.push("efgh");

        String ret2 = myStack2.peek();
        System.out.println(ret2);//efgh
    }
}

泛型的边界

  • 如果没有给上界,被擦除为 Object,如果有上界,被擦除为上界

  • class Generic<T extends Comparable> 中

  •      <T extends Comparable<T>> 中 Comparable是上界
    
  •      T 一定是实现了 Comparable 接口的(T 一定是 Comparable的子类)
    
  •      T 被擦除为 Compareble
    
  • 泛型没有 下界

写一个泛型类,包含一个方法,该方法是找到数组当中的最大值

class Generic<T extends Comparable<T>> {
    public T maxNum(T[] array) {
        T maxNum = array[0];
        for (int i = 1; i < array.length; i++) {
            if (maxNum .compareTo(array[i]) < 0) {
                maxNum = array[i];
            }
        }
        return maxNum;
    }
}
  • 类型推导 -》 根据实参的类型推导出形参的类型
class Generic2 {
    public static <T extends Comparable<T>> T maxNum(T[] array) {
        T maxNum = array[0];
        for (int i = 1; i < array.length; i++) {
            if (maxNum .compareTo(array[i]) < 0) {
                maxNum = array[i];
            }
        }
        return maxNum;
    }
}

public class TestDemo3 {

    public static void main(String[] args) {
        Integer[] array = {12,3,4,5,1,15,20,8};
        System.out.println(Generic2.maxNum(array));
        String[] str = {"abc","defg","hij"};
        System.out.println(Generic2.maxNum(str));
    }

    public static void main1(String[] args) {
        Generic<Integer> generic = new Generic<>();
        Integer[] array = {12,3,4,5,1,15,20,8};
        System.out.println(generic.maxNum(array));
    }
}
  • 通配符 : 也是一种泛型
  • 通配符一般用于读取 add(?)
  • 泛型一般用于写入 add(T)
  • 通配符既有上界 ,也有下界
  • <? extends 上界> ? 一定是上界的子类
  • <? super 下界> ? 一定是下界的父类

泛型中的父子类型

  • Object 是 所有的 父类
  • 但 MyArrayList 不是 MyArrayList 的父类
  •      因为 泛型 中 <> 里的东西 不参与类型的组成
    
  • 需要通过 通配符 来确定父子类型
  •      MyArrayList<?> 是 MyArrayList<? extends Number> 的父类型
    
  •      MyArrayList<? extends Number> 是 MyArrayList<Integer> 的父类型
    
//一个通用方法,打印集合中所有元素
class GenerList {
    public static <T> void printList(ArrayList<T> list) {
        for (T ret:list) {
            System.out.print(ret + " ");
        }
        System.out.println();
    }

    //<?> : 通配符
    public static void printList2(ArrayList<?> list) {
        for (Object ret:list) {
            System.out.print(ret + " ");
        }
        System.out.println();
    }
}
public class TestDemo4 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        GenerList.printList(list);
        GenerList.printList2(list);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值