如何用更具体的类型替换参数化类型

How to replace a parameterized type with a more specific one

考虑以下设置:

我们有一个接口SuperType,它的参数化如下:

public interface SuperType<V> {
}

SuperType 支持方法链接。因此它定义了另一个类型参数,它捕获每个方法返回的具体实现子类型,如下所示:

public interface SuperType<V, S extends SuperType<V, S>> {

    public S doSomething();
}

让我们考虑 SuperType<V, S extends SuperType<V, S>>:

的实现
public class SubType<V> implements SuperType<V, SubType<V>> {

    private final V value;

    public SubType(V value) { this.value = value; }

    public SubType<V> doSomething() { return this; }
}

有人实例化 SubType<V> 使用例如字符串,但为类型参数 V:

提供 Object
Object s = "Java Generics";
SubType<Object> x = new SubType<>(s);

现在我们要为 SuperType<V, S extends SuperType<V, S>> 定义另一个方法,它采用更具体的类型参数 V 和 returns 相同的实现类型 S 但现在参数化为W extends V:

public interface SuperType<V, S extends SuperType<V, S>> {

    public S doSomething();

    public <W extends V, T extends SuperType<W, T>> T doMoreSpecific(Class<W> typeToken);
}

这个新的接口定义旨在支持:

Object s = "Java Generics";
SubType<Object> x = new SubType<>(s);
SubType<String> y = x.doMoreSpecific(String.class);

这里我努力实现SubType<V>。我想提供的实现是:

public class SubType<V> implements SuperType<V, SubType<V>> {

    private final V value;

    public SubType(V value) { this.value = value; }

    public SubType<V> doSomething() { return this; };

    public <W extends V> SubType<W> doMoreSpecific(Class<W> typeToken) {
        return new SubType<>((W) value);
    }
}

我的问题是:

我应该如何在类型 SuperType<V, S extends SuperType<V, S>> 中定义方法 doMoreSpecific() 的签名,以便由 SubType<V> implements SuperType<V, SubType<V>> 可以接受吗?

否则,哪个实现和接口方法定义可以解决问题?

否则,为什么我们不能在 Java 中做到这一点?

使用以下签名:

<W extends V> SuperType<W, ?> doMoreSpecific(Class<W> typeToken);

可能还有一些不安全的情况,虽然我还没有找到,欢迎大家批评指正!

你不能按照你的代码建议的去做,你有一个类型化的方法W是从参数类型推断出来的,但是参数类型是仅在呼叫站点知道。即没有 one 版本的方法 doMoreSpecific() 可以在接口 SuperType 中指定(必须实现)。

最接近的方法是使 W 成为 SuperType 的泛型类型参数,但这样一来,您的实现将只适用于一个 class W,即会是 typeToken 多余的,这显然不是你想要的。

我不知道你在 doMoreSpecific 背后的确切意图是什么,但如果它只是从 SubType<Object> 转换为 SubType<String> 你可以执行以下操作(尽管这不是很好的练习...):

Object s = "Java Generics";
SubType<Object> x = new SubType<>(s);
SubType<String> y = (SubType<String>) (SubType<?>) x;

注意 1:这仍然会给出警告。

注意 2:如果 s 不是 String 类型,此转换甚至会起作用!但是当你调用 y.doSomething() 时你会得到一个 ClassCastException。 (你也会在你的例子中得到这种情况)。

例如:以下一段(丑陋的)代码也可以工作(突出显示的行除外):

    ArrayList<?> lo = new ArrayList<Object>();
    ArrayList<Integer> li = (ArrayList<Integer>) lo;
    ArrayList<String> ls = (ArrayList<String>) lo;

    li.add(5);
    ls.add("five");

    System.out.println(lo);        // prints "[5, five]"

    System.out.println(li.get(0)); // prints "5"
    System.out.println(li.get(1)); // ClassCastException

    System.out.println(ls.get(0)); // ClassCastException
    System.out.println(ls.get(1)); // prints "five"

注释 3:这完美地展示了泛型的实际工作原理:它们所做的只是自动为您在所需位置插入强制转换。