如果Class <type>使用,如何获得匹配的泛型类型?扩展Type?</type>

时间:2013-09-21 18:22:17

标签: java generics

鉴于这个原型:

public static <C extends SetComposite, S extends Iterable<U>, U> boolean
eligibleForInclusion(C me, Class<C> clazz, List<S> members) throws TypeCollisionException {
    ...
}

这个电话网站:

public void include(RestaurantSet member) {
    if (SetCompositeUtils.<RestaurantSetComposite, RestaurantSet, Restaurant>
            eligibleForInclusion(this, this.getClass(), Arrays.asList(member))) {
        inc.add(member);
    }
}

Eclipse编译器给出了这个错误:

The parameterized method <RestaurantSetComposite, RestaurantSet, Restaurant>eligibleForInclusion(RestaurantSetComposite, Class<RestaurantSetComposite>, List<RestaurantSet>) of type SetCompositeUtils is not applicable for the arguments (RestaurantSetComposite, Class<capture#1-of ? extends RestaurantSetComposite>, List<RestaurantSet>)

如果我正确阅读,则Class<capture#1-of ? extends RestaurantSetComposite>Class<RestaurantSetComposite>不匹配。我可以以某种方式解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

由于您的this可能是子类的实例,this.getClass()可能不完全是RestaurantSetComposite.class,因此您需要

public static <C extends SetComposite, S extends Iterable<U>, U> boolean
eligibleForInclusion(C me, Class<? extends C> clazz, List<S> members) throws TypeCollisionException {
    ...
}