把它放到一个实例中:
public class Inheritance {
public static class A<T,U,V>{
T t;
U u;
V v;
A(T t, U u, V v) {
this.t = t;
this.u = u;
this.v = v;
}
T getT() {return t;}
U getU() {return u;}
V getV() {return v;}
}
public static class B<T,U,V> extends A<T,U,T>{
public B(T t, U u, V v) {
super(t, u ,t);
}
}
public static void main(String[] args) {
B<Boolean, Integer, String> b = new B<>(false, 1, "string");
// 't' attribute is Boolean
// since type parameter T of class B is Boolean
Boolean t = b.getT();
// 'v' attribute is Boolean
// since type parameters T and V of class A must have the same type as
// type parameter T of class B
Boolean v = b.getV();
}
}
基本上,B 类扩展了 A 类(具有三个通用参数)。通过声明B<T,U,V> extends A<T,U,T>,您只需将 A 的第一个和 A 的第三个通用参数绑定到 B 的第一个参数的相同类型
如示例所示,在 B 类的构造函数中,我们有三种不同的类型 - Boolean、Integer、String,但在 A 类的构造函数中,我们只有两种不同的类型 Boolean、Integer,因为 A 类的第一个和第三个构造函数参数都是绑定到布尔类型
更多关于泛型和继承的信息可以在这里找到:
https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html