문제

I have the following simple interface:

public interface ISimmilarityMeasure<T extends ResourceDescriptor> {
    public double getSim(T s, T t);
}

and implementations like

public class NormalizedLevenstheinSim implements
             ISimmilarityMeasure<SimpleResourceDescriptor> { ... }

and

public class JaccardCommentsSim implements
             ISimmilarityMeasure<LabelsCommentsResourceDescriptor> { ... }

Both SimpleResourceDescriptor and LabelsCommentsResourceDescriptor extend

public abstract class ComparableResourceDescriptor
             implements ResourceDescriptor 

At runtime, I call the method

public static ISimmilarityMeasure<? extends ResourceDescriptor> getSimInstance(){ }

which will return an instance "sim" of ISimmilarityMeasure that relies on a specific instance of ResourceDescriptor.

I also create an array ResourceDescriptor[] candidates which will hold, at runtime, instances of the ResourceDescriptor type required by the specific ISimmilarityMeasure object.

However, if I try to call sim.getSim(candidates[0], candidates[1]) the compiler tells me that

"capture#3-of ? extends ResourceDescriptor ... is not applicable for the arguments (ResourceDescriptor ... "

I use eclipse and if I look at the available methods for sim, it shows me getSim(null s, null t). I do not understand why this is the case. Should it not be clear to the compiler, that getSim has to expect any ResourceDescriptor and that every object in candidates is a ResourceDescriptor and therefore allow the call? Should it not be an exception at runtime if the specific ISimmilarityMeasure expects a certain type of ResourceDescriptor but is handed a different one?

도움이 되었습니까?

해결책

getSimInstance() will return an object of type ISimmilarityMeasure<X> for some type X. All we know about X is that it inherits from ResourceDescriptor. On this object, you call getSim(ResourceDescriptor, ResourceDescriptor). However, it's not expecting ResourceDescriptor parameters, it's expecting X parameters.

While an X is always a ResourceDescriptor, there's no guarantee that a ResourceDescriptor is an X, thus your compiler refuses to accept it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top