2

给定这个原型:

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>。我能以某种方式解决这个问题吗?

4

1 回答 1

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 {
    ...
}
于 2013-09-21T19:03:52.703 回答