Java-使用方法继承覆盖问题

 收藏

我有对象继承的下一个问题:

-头等舱:

public class LegalPersonRest extends LegalPerson {}

-第二类:

public class Company {

private List<LegalPerson> legalPersons;

}

-第三类:

public class CompanyRest extends Company {

@Override
public List<LegalPersonRest> getLegalPersons() {
    return super.getLegalPersons();
}

}

在这种方法getLegalPersons中,我得到了错误:尝试使用不兼容的返回类型。

我不知道为什么LegalPersonRest扩展LegalPerson。

公司和LegalPerson类是我的领域。 (层域)

CompanyRest和LegalPersonRest类扩展了域的类。 (层适配器)

如果我将List更改为List,我认为是错误的,因为我需要使用LegalPersonRest。

这种情况下正确的继承形式是什么?

问候,

回复
  • I'm just going to point out in case this was the issue: the super class needs to have the method you are overriding. Company doesn't have the method getLegalPersons(), so you can't override it. But I think Company does have getLegalPersone() because you call that method later (please produce a minimal, reproducible example). The next thing I can think of is that Company's getLegalPersons() method doesn't return a List<LegalPersonRest>. It might return a list of something else, or another object entirely. Please check the return type to see if it is the same return type as the method in CompanyRest.