20180910-Generics泛型

  • abstract over type
  • type parameter类型参数
  • compile-time
public interface List {
    void add(E x);
    Iterator iterator();
}
  • formal type parameters
  • actual type argument
  • A generic type declaration is compiled once and for all,
  • List不是Listsubtype
  • wildcard type
  • Collection (pronounced "collection of unknown")
  • 使用generic method解决compile-time error
  • The compiler infers the most specific type argument
  • erasure类型擦除
  • 泛型类Class不包含泛型参数。运行时没有类型参数。
  • 不能新建泛型数组。
  • ? extends T? super T
  •  T[] makeArray(T t) {
        return new T[100]; // Error.
    }
    

    In general, if you have an API that only uses a type parameter T as an argument, its uses should take advantage of lower bounded wildcards (? super T). Conversely, if the API only returns T, you'll give your clients more flexibility by using upper bounded wildcards (? extends T).

    • multiple bounds
    • T1 & T2 ... & Tn

    A type variable with multiple bounds is known to be a subtype of all of the types listed in the bound.

    你可能感兴趣的:(20180910-Generics泛型)