zoukankan      html  css  js  c++  java
  • 泛型通配符的含义

    泛型,就是允许在定义类、接口、方法时使用类型形参,这个类型形参将在声明变量、创建对象、调用放法事动态地指定。

    泛型存在的一个好处就是,如果我们不小心使用了其他非指定类型参数,在编译时期就能提醒。

    泛型接口

    public interface List<E> {
        //在该接口中,E可作为任意类型使用
        void add(E x);
        Iterator<E> iterator();
        ...
    }
    

    泛型类

    public class Apple<T> {
        // 使用 T 类型形参定义实例变量
        private T info;
        // 使用 T 类型形参定义构造器
        public Apple(T info){
            this.info = info;
        }
        ...
        
        public static void main(String[] args){
            // 传给 T 形参为 String 类型
        	Apple<String> a1 = new Apple<>("苹果");
            
            // 传给 T 形参为 Double 类型
            Apple<Double> a2 = new Apple<>(1.23);
        }
        
    }
    

    经常看到泛型中的 E T V ? 等通配符不知道是什么含义,今天去查了一下。

    类型通配符的含义

    E ----- Element(元素 ,集合中使用)
    T ----- Type(类型 ,类)
    K ----- key(键)
    V ----- Value(值)
    N ----- Number(数值类型)
    ? ----- 不确定的java类型

    <? extends T> <? super T>

    <? extends T> 表示类型的上界,参数类型是T或T的子类。

    <? super T>表示类型下界,参数类型是T或T的父类型。

    如果频繁往外读取内容,适合用上界Extends。

    如果频繁插入内容,适合用下界super。

  • 相关阅读:
    A visual proof that neural nets can compute any function 2
    Matrix
    Formula
    ID and CLASS
    hugeng007_diary01_the living way
    the mathematical knowledge
    sys.argv[]
    The Convolutional Networks
    DVWA之XSS (跨站脚本攻击)存储型+反射型。
    DVWA之 File Inclusion 文件包含
  • 原文地址:https://www.cnblogs.com/luler/p/15010731.html
Copyright © 2011-2022 走看看