Java-为什么我被迫将引用构造函数转换为Supplier,否则Java声称该方法不明确?

问题描述 投票:1回答:1

我具有以下定义的接口,打算由3d向量的所有实现方式实现:

public interface IVector3<T extends Number> extends IVector<T> {
    // Other stuff

    public static <T extends Number, V extends IVector3<T>> V cross(V target, IVector3<?> vector0, IVector3<?> vector1) {
        // Implementation
    }

    public static <T extends Number, V extends IVector3<T>> V cross(Supplier<V> factory, IVector3<?> vector0, IVector3<?> vector1) {
        return cross(factory.get(), vector0, vector1);
    }
}

我还定义了以下接口实现:

public class Vector3f extends Vectorf implements IVector3<Float> {
    // Other stuff

    public static Vector3f cross(IVector3<?> vector0, IVector3<?> vector1) {

        // The annoying required cast
        return IVector3.cross((Supplier<Vector3f>) Vector3f::new, vector0, vector1);
    }
}

所以,为什么Java要求我对引用构造函数进行强制转换,否则它声称对方法的调用是模棱两可的?

有界类型参数不能解决歧义吗?

最后,是否有任何解决方案可以避免在不更改有界类型参数的情况下强制转换引用构造函数?

Note:层次结构中没有其他类/接口定义名为“ cross”的方法(是否静态)。

注2:我正在使用Java 12。

java methods overloading overload-resolution functional-interface
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.