有以下类(class):

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);

我收到以下编译错误:



但为什么? Human扩展了LivingBeing

最佳答案

您的validate方法声明必须用LivingBeingWrapper参数化的LivingBeing调用它。但是,您传入的是LivingBeingWrapper参数化的Human。尝试从此更改方法声明:

public boolean validate(LivingBeingWrapper<LivingBeing> livingBeingWrapper)

对此:
public boolean validate(LivingBeingWrapper<? extends LivingBeing> livingBeingWrapper)

09-05 09:23