繁体   English   中英

泛型函数调用未在Java 8中编译,扩展了多个接口

[英]Generics function call not compiling in java 8 extending multiple interfaces

运行Java 1.8 JavaSE-1.8(jdk1.8.0_20)

这节课:

public class SimpleQuestion {

    public static void main(String[] args) {
        DoNothing();
        DoNothing2();
        DoNothing3();
        DoNothing4();
    }    

    public interface Interface1 {
        public void go();
    }

    public interface Interface2<X> {
        public X go2();
    }

    private static <X, T extends Interface2<X> & Interface1> void DoNothing() {
        return;
    }

    private static <X, T extends Interface2 & Interface1> void DoNothing2() {
        return;
    }

    private static <X, T extends Interface2<X>> void DoNothing3() {
        return;
    }

    private static <T extends Interface2<T> & Interface1> void DoNothing4() {
        return;
    }    

}

给出编译错误:

类型SimpleQuestion中的方法DoNothing()不适用于参数()

为什么是那个而不是DoNothing2,3和4?

错误消息似乎是指规范18.5.1节中定义的算法失败。

对于DoNothing,算法按以下步骤进行(使用来自上面链接的术语):

  • 类型参数是

    P1 = X

    P2 = T extends Interface2<X> & Interface1

    我将使用a1和a2作为相应的推断变量。

  • 初始边界集为

    B0 = {a1 <: Object, a2 <: Interface2<a1>, a2 <: Interface1}

  • 没有参数,因此此时没有添加额外的界限(B2 = B0)。

  • a2对a1有依赖性,因此我们尝试首先解析a1。 它具有适当的Object上限,因此我们将其实例化。 合并a1 = Object涉及添加边界

    a2 <: Interface2<Object>

  • 接下来,我们解决a2。 现在有两个适当的上限,因此我们将a2实例化为它们的glb:

    a2 = Interface2<Object> & Interface1

  • 现在每个变量都有一个实例化,因此解析成功。

因此,与错误消息相反,DoNothing的调用应该适用。 这似乎是Java编译器中的错误。

暂无
暂无

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

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