通用:ArrayList中的?扩展ISomeInterface在Java中 [英] Generic: ArrayList of ? Extends ISomeInterface in Java
本文介绍了通用:ArrayList中的?扩展ISomeInterface在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有在以下code一些麻烦。
I'm having some trouble in the following code.
public ArrayList<? extends IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
ArrayList<? extends IEvent> returnedEvents = new ArrayList<GoogleEvent>();
returnedEvents.add(new GoogleEvent());
return (returnedEvents);
}
这返回下面的编译错误的returnedEvents.add(新GoogleEvent());的code线:
This return the following compilation error for the "returnedEvents.add(new GoogleEvent()); line of code":
Add方法的类型(捕获#1-的?扩展IEvent)
ArrayList是不适用于
参数(GoogleEvent)
The method add(capture#1-of ? extends IEvent) in the type ArrayList is not applicable for the arguments (GoogleEvent)
类的声明 GoogleEvent
如下:
public class GoogleEvent implements IEvent {...}
我知道有一些棘手的部分使用泛型在Java中,因此野生卡,但我似乎无法想出解决办法。
I know there are some tricky parts using generics in Java, thus the wild-cards but I can't seem to figure this out.
感谢。
推荐答案
你为什么不写:
public List<IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
List<IEvent> returnedEvents = new ArrayList<IEvent>();
returnedEvents.add(new GoogleEvent());
return returnedEvents;
}
这篇关于通用:ArrayList中的?扩展ISomeInterface在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文