泛型方法

泛型方法,方法拥有自己独立的泛型类型

<T> T set(T t) {
    return t;
}

此泛型类型与类里面尖括号的泛型类型无关,是独立的,好处是可以做类型检查与类型强转,比如我们的Android的findviewbyId()

@Nullable
public <T extends View> T findViewById(@IdRes int id) {
    return getWindow().findViewById(id);
}
<p>TextView text = findViewById(R.id.text)
通过TextView text来推断T的类型是TextView,十分方便