如何调用具有Interface参数的方法? [重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何调用具有Interface参数的方法? [重复]相关的知识,希望对你有一定的参考价值。

我有一个接口VersionInterface,由我的对象ObjectDefinition实现

我也有一个接受两个List<VersionInterface>作为参数的方法。

由于ObjectDefinition实现了VersionInterface,为什么我不能将我的两个List<VersionInterface>as参数传递给我的方法?

方法定义:

public List<VersionInterface> updateArtifact(List<VersionInterface> currentCopy, List<VersionInterface> updatedCopy)

ObjectDefinition定义:

public class ObjectDefinition implements VersionInterface {

我怎么称updateArtifact

service.updateArtifact(currentCopy, updatedCopy);

currentCopyupdatedCopy都是ArrayList<ObjectDefinition>

我明白了:

The method updateArtifact(List<VersionInterface>, List<VersionInterface>) in the type ORService is not applicable for the arguments (List<ObjectDefinition>, List<ObjectDefinition>)

编辑:我的问题与接口而不是子类有关

答案

您不能这样做,因为方法原型指定它返回VersionInterface对象的List,而不是ObjectDefinition对象的List。

正如您所演示的,VersionInterface是一个接口。也许ObjectDefinition是唯一实现该接口的类,但通常在Java中不是这种情况。 VersionInterface可能有多个实现,返回的List可能包含各种不同类型的对象。

以上是关于如何调用具有Interface参数的方法? [重复]的主要内容,如果未能解决你的问题,请参考以下文章