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

时间:2015-01-02 11:26:09

标签: java generics

我正在努力制作一个有点强大的类型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);

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

1 个答案:

答案 0 :(得分:1)

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

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

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