简体   繁体   English

带参数的Java泛型 - 继承

[英]Java generics with parameter - inheritance

I have the following code: 我有以下代码:

public class Inheritance {

    class A<T,U,V>{

    }

    class B<T,U,V> extends A<T,U,T>{        

    }
}

Could somebody explain me, how it actually works? 有人可以解释一下,它究竟是如何运作的? Does the Class B extend only A class, which parameters are "T,U,T", or it extends the actual A"T,U,V" class? B类是否只扩展A类,参数是“T,U,T”,还是扩展了实际的A“T,U,V”类?

A 's T = B 's T AT = BT
A 's U = B 's U AU = BU
A 's V = B 's T AV = BT

B<String, Integer, Void> b = null;
A<String, Integer, String> a = b;

Does the Class B extend only A class, which parameters are "T,U,T" B类是否只扩展A类,参数为“T,U,T”

Yes, exactly. 对,就是这样。

or it extends the actual A"T,U,V" class? 或者它扩展了实际的A“T,U,V”类?

It's a template, there are no actual generic parameters. 它是一个模板,没有实际的通用参数。 It's a way to match your own parameter types with the parent's ones. 这是一种匹配您自己的参数类型与父类参数类型的方法。 If there is a B<String, Integer, Long> instance, there will be a parent object A<String, Integer, String> backing it. 如果有一个B<String, Integer, Long>实例,则会有一个父对象A<String, Integer, String>支持它。

Let put it into real example: 让我们把它放到真实的例子中:

 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(); 
        }
    }

Basically class B extends class A (which has three generic params). 基本上B类扩展了A类(它有三个通用参数)。 By declaring B<T,U,V> extends A<T,U,T> you just bind the A's first and A's third generic param to the same type of B's first param 通过声明B<T,U,V> extends A<T,U,T>你只需将A的第一个和第三个通用参数绑定到同一类型的B的第一个参数上

As shown in example in constructor of class B we have three distinct types - Boolean, Integer, String, but in constructor of class A we have only two distinct types Boolean, Integer because 1st and 3th constructor param of class A are both bound to Boolean type 如类B的构造函数中的示例所示,我们有三种不同的类型 - Boolean,Integer,String,但是在类A的构造函数中,我们只有两个不同的类型Boolean,Integer因为类A的第1和第3个构造函数参数都绑定到Boolean类型

More on generics and inheritence can be found here: https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html 有关泛型和继承的更多信息,请访问: https//docs.oracle.com/javase/tutorial/java/generics/inheritance.html

In this code, the templated type " identifiers " for each class are not linked to each other, let me modify the snippet to explain what I mean : 在这段代码中,每个类的模板化类型“ 标识符 ”没有相互链接,让我修改片段来解释我的意思:

public class Inheritance {
    class A<T,U,V> { }

    class B<I,J,K> extends A<I,J,I>{ }
}

The code is the same as before. 代码与以前相同。 You can see that there is no "naming" correlation between I,J,K and T,U,V. 您可以看到I,J,K和T,U,V之间没有“命名”关联。

Here the types I and J are forwarded to A. From A's perspective, a substitution is made with : T=I, U=J, V=I. 这里,类型I和J被转发到A.从A的角度来看,替换是:T = I,U = J,V = I.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM