Java 如何调用具有接口参数的方法?
我有一个接口Java 如何调用具有接口参数的方法?,java,interface,Java,Interface,我有一个接口VersionInterface,它由我的对象ObjectDefinition 我还有一个方法,它接受两个List作为参数 既然ObjectDefinition实现了VersionInterface,为什么我不能将我的两个列表作为参数传递给我的方法呢 方法定义: public List<VersionInterface> updateArtifact(List<VersionInterface> currentCopy, List<VersionInte
VersionInterface
,它由我的对象ObjectDefinition
我还有一个方法,它接受两个List
作为参数
既然ObjectDefinition
实现了VersionInterface
,为什么我不能将我的两个列表作为参数传递给我的方法呢
方法定义:
public List<VersionInterface> updateArtifact(List<VersionInterface> currentCopy, List<VersionInterface> updatedCopy)
public class ObjectDefinition implements VersionInterface {
service.updateArtifact(currentCopy, updatedCopy);
我如何呼叫updateArtifact
:
public List<VersionInterface> updateArtifact(List<VersionInterface> currentCopy, List<VersionInterface> updatedCopy)
public class ObjectDefinition implements VersionInterface {
service.updateArtifact(currentCopy, updatedCopy);
currentCopy
和updatedCopy
都是ArrayList
我得到:
类型或服务中的方法updateArtifact(列表,列表)不适用于参数(列表,列表)
编辑:
我的问题与接口有关,而不是子类。您不能这样做,因为方法原型指定它返回的是VersionInterface对象列表,而不是ObjectDefinition对象列表
正如您所演示的,VersionInterface是一个接口。也许ObjectDefinition是实现该接口的唯一类,但在Java中通常不是这样。VersionInterface可能有多个实现,返回的列表可能包含各种不同类型的对象 我相信现有的帖子回答了这个问题。我的例子使用接口,不涉及扩展