【问题标题】:Java generics with parameter - inheritance带参数的 Java 泛型 - 继承
【发布时间】:2019-04-14 10:19:10
【问题描述】:

我有以下代码:

public class Inheritance {

    class A<T,U,V>{

    }

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

    }
}

有人可以解释一下,它实际上是如何工作的吗? B类是只扩展了A类,哪些参数是“T,U,T”,还是扩展了实际的A“T,U,V”类?

【问题讨论】:

    标签: java generics inheritance subclass subtype


    【解决方案1】:

    A's T = B's T
    A's U = B's U
    A's @987654 @=BT

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

    【讨论】:

      【解决方案2】:

      B类是否只扩展A类,参数为“T,U,T”

      是的,没错。

      或者它扩展了实际的 A"T,U,V" 类?

      这是一个模板,没有实际的泛型参数。这是一种将您自己的参数类型与父参数类型匹配的方法。如果有B&lt;String, Integer, Long&gt; 实例,就会有一个父对象A&lt;String, Integer, String&gt; 支持它。

      【讨论】:

        【解决方案3】:

        把它放到一个实例中:

         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&lt;T,U,V&gt; extends A&lt;T,U,T&gt;,您只需将 A 的第一个和 A 的第三个通用参数绑定到 B 的第一个参数的相同类型

        如示例所示,在 B 类的构造函数中,我们有三种不同的类型 - Boolean、Integer、String,但在 A 类的构造函数中,我们只有两种不同的类型 Boolean、Integer,因为 A 类的第一个和第三个构造函数参数都是绑定到布尔类型

        更多关于泛型和继承的信息可以在这里找到: https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html

        【讨论】:

        • 请你解释一下你打算用你的例子展示什么?不明显。
        • 是的,我更新了我的答案并注释掉了这个例子
        【解决方案4】:

        在这段代码中,每个类的模板类型“identifiers”并没有相互链接,让我修改sn-p来解释我的意思:

        public class Inheritance {
            class A<T,U,V> { }
        
            class B<I,J,K> extends A<I,J,I>{ }
        }
        

        代码和以前一样。可以看到 I,J,K 和 T,U,V 之间没有“命名”相关性。

        这里类型 I 和 J 被转发给 A。从 A 的角度来看,替换为:T=I, U=J, V=I。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-06
          • 1970-01-01
          • 2021-10-11
          相关资源
          最近更新 更多