gpt4 book ai didi

java - 类泛型的方法泛型绑定(bind)限制

转载 作者:行者123 更新时间:2023-11-30 11:10:18 26 4
gpt4 key购买 nike

我正在尝试制作一个有点强类型的 API:

class PropertyType<T> { ... }
class SubType1<T> extends PropertyType<T> { ... }
class SubType2<T> extends PropertyType<T> { ... }

class PropertyHandler<K extends PropertyType<?>> {

// Here, the V generics is not related to the property type,
// hence the value is not typed
public <V> getValue(K prop) { ... }

// In this case, we have correct typing, but T is not bounded to K
public <V, T extends PropertyType<V>> getValue(T prop) { ... }

// This would be a solution, but it doesn't compile because K is a type variable
public <V, T extends K & PropertyType<V>> getValue(T prop) { ... }
}

这就是它的用途:

PropertyHandler<SubType1<?>> ph = new PropertyHandler<>();

// I want to allow this
SubType1<Integer> p1 = new SubType1<>();
SubType1<Boolean> p2 = new SubType1<>();
Integer v1 = ph.getValue(p1);
Boolean v2 = ph.getValue(p2);

// I don't want to allow this
SubType2<Integer> p3 = new SubType2<>();
Boolean v3 = ph.getValue(p3);

有办法解决吗?也许这是一些可以以不同方式处理的架构问题?

最佳答案

getValue() 的返回类型必须添加到PropertyHandler 的通用签名中。否则,PropertyHandler 无法从 PropertyType 的通用定义中推断出 getValue() 返回类型。

因此,要么将返回类型和属性类型都分配给 PropertyHandler 泛型签名,要么通过在 PropertyHandler 中指定返回类型来翻转它,并让 PropertyHandler 确保正确的 PropertyType 参数类型,如下所示:

class PropertyType<T, H extends PropertyHandler<T>> {
public T getValue() { ... }
}
class IntegerType extends PropertyType<Integer, PropertyHandler<Integer>> { ... }
class BooleanType extends PropertyType<Boolean, PropertyHandler<Boolean>> { ... }

class PropertyHandler<V> {
public <T extends PropertyType<V, PropertyHandler<V>>> V getValue(T prop) {
return prop.getValue();
}
}

这是你将如何使用它:

PropertyHandler<Integer> ph = new PropertyHandler<>();
IntegerType p1 = new IntegerType();
Integer v1 = ph.getValue(p1); // works fine
BooleanType p3 = new BooleanType();
Boolean v3 = ph.getValue(p3); // compile time error

关于java - 类泛型的方法泛型绑定(bind)限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com