转换Collection< MyType>收集<对象>

编程入门 行业动态 更新时间:2024-10-28 14:36:01
本文介绍了转换Collection< MyType>收集<对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

public UniqueField(Collection< Object> items){ this.items =物品; }

Collection< Object> 是我可以使用集合< OtherType> 。

在做:

集合< OtherType> collection = ... UniqueField(collection);

我得到无效参数的编译错误。如何解决这个问题?

解决方案

您必须使用它而不是

public UniqueField(Collection< ;? extends Object> items){ this.items = items; }

或?因为它等于?extends Object

public UniqueField(Collection<> items){ this .items =物品; }

你可以看到这里为原因

I have a class with the following constructor:

public UniqueField(Collection<Object> items) { this.items=items; }

The idea behind the Collection<Object> is that I would be able to use Collection<OtherType>.

When doing:

Collection<OtherType> collection=... new UniqueField(collection);

I getting a compile error of invalid argument. How can I fix this?

解决方案

You have to use this instead

public UniqueField(Collection<? extends Object> items) { this.items=items; }

or ? because it is equal to "? extends Object"

public UniqueField(Collection<?> items) { this.items=items; }

You can see here for the reason

更多推荐

转换Collection&lt; MyType&gt;收集&lt;对象&gt;

本文发布于:2023-11-07 07:01:50,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   amp   Collection   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!