方法泛型将限制与类泛型绑定



我正在尝试制作一个有点强大的类型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

最新更新