我正在尝试制作一个强度较高的类型化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无法从getValue()的通用定义中推断PropertyType返回类型。

因此,可以将返回类型和属性类型都分配给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

09-25 22:30