有以下课程:
public class LivingBeing { … }
那就是
public class Human extends LivingBeing { … }
现在还有这个包装器:
public class LivingBeingWrapper<T extends LivingBeing> { … }
并且为了完成图片,还有方法
public boolean validate(LivingBeingWrapper<LivingBeing> livingBeingWrapper)
现在编写以下代码时
LivingBeingWrapper<Human> wrapper = createHumanWrapper();
validate(wrapper);
我得到以下编译错误:
The method validate(LivingBeingWrappe<LivingBeing>
livingBeingWrapper) in the type MyType is not applicable for the
arguments (LivingBeingWrapper<Human>).
但为什么?人类扩展了LivingBeing.
解决方法:
您的validate方法声明必须使用LivingBeing参数化的LivingBeingWrapper调用它.但是,您正在传递一个使用Human参数化的LivingBeingWrapper.尝试更改此方法声明:
public boolean validate(LivingBeingWrapper<LivingBeing> livingBeingWrapper)
对此:
public boolean validate(LivingBeingWrapper<? extends LivingBeing> livingBeingWrapper)