Can’t convert java.util.List to java.lang.Iterable of template interface.

huangapple go评论113阅读模式
英文:

Can't convert java.util.List to java.lang.Iterable of template interface

问题

我在使用一个实现接口的对象列表作为该接口的可迭代对象时遇到了问题。

我的类定义如下:

```java
class BaseObject implements AInterface<OtherObject> {...}

class Foo extends BaseObject{...}

interface BInterface{
void doSomething(AInterface<OtherObject> a);
void doSomethingToIterable(Iterable<AInterface<OtherObject>> a);
}

无法编译通过的代码如下:

private BInterface bInterface;
...

public void baa(List<Foo> list){
   bInterface.doSomethingToIterable(list);//这行代码导致编译错误
   ...
}

我从Maven获取的编译错误信息如下:

不兼容的类型:java.util.List<my.package.Foo> 无法转换为java.lang.Iterable<my.other.package.AInterface<my.third.package.OtherObject>>

我可以对列表中的每个元素调用doSomething。实际上,我可以通过内联实现doSomethingToIterable来实现这一点。

在终端中运行java -version会产生以下输出:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252_b09)
OpenJDK 64-Bit Server VM (build 1.8.0_252_b09)

我理解的是,由于List扩展了Collection,而Collection扩展了Iterable,而Foo扩展了实现了AInterfaceBaseObject,所以这两种类型应该是兼容的。但似乎我的理解是错误的。

为什么会出现上述错误,如何修复它?


<details>
<summary>英文:</summary>

I&#39;m having an issue using a list of objects implementing an interface as an iterable of that interface.

My class definitions are like this:

```java
class BaseObject implements AInterface&lt;OtherObject&gt; {...}

class Foo extends BaseObject{...}

interface BInterface{
void doSomething(AInterface&lt;OtherObject&gt; a);
void doSomethingToIterable(Iterable&lt;AInterface&lt;OtherObject&gt;&gt; a);
}

The code that won't compile is

private BInterface bInterface;
...

public void baa(List&lt;Foo&gt; list){
   bInterface.doSomethingToIterable(list);//This is the line that gives a compilation error
   ...
}

The compilation error I'm getting from maven is

incompatible types: java.util.List&lt;my.package.Foo&gt; cannot be converted to java.lang.Iterable&lt;my.other.package.AInterface&lt;my.third.package.OtherObject&gt;&gt;

I can invoke doSomething to each element in the List. In fact, I can do this by inlining the implementation of doSomethingToIterable.

Running java -version in terminal produces the output

openjdk version &quot;1.8.0_252&quot;
OpenJDK Runtime Environment (build 1.8.0_252_b09)
OpenJDK 64-Bit Server VM (build 1.8.0_252_b09)

My understanding is that since List extends Collection which extends Iterable, and Foo extends BaseObject that implements AInterface, these two types should be compatible. It seems my understanding is wrong.

Why am I getting the above error, and how can it be fixed?

答案1

得分: 3

List&lt;Subclass&gt; 不是 List&lt;Superclass&gt; 的实例。

尝试:

void doSomethingToIterable(Iterable&lt;? extends AInterface&lt;OtherObject&gt;&gt; a)
英文:

List&lt;Subclass&gt; is not an instance of List&lt;Superclass&gt;.

Try:

void doSomethingToIterable(Iterable&lt;? extends AInterface&lt;OtherObject&gt;&gt; a)

huangapple
  • 本文由 发表于 2020年8月6日 15:06:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63278468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定