通用: ArrayList 的 ?在 Java 中扩展 ISomeInterface

发布于 2024-12-10 21:16:36 字数 683 浏览 0 评论 0原文

我在以下代码中遇到一些问题。

public ArrayList<? extends IEvent> getEventsByDateRange(DateTime minStartTime,  DateTime minEndTime) 
{
    ArrayList<? extends IEvent> returnedEvents = new ArrayList<GoogleEvent>();
    returnedEvents.add(new GoogleEvent());
    return (returnedEvents);
}

这将返回“returnedEvents.add(new GoogleEvent()); 代码行”的以下编译错误:

类型中的方法add(capture#1-of ? extends IEvent) ArrayList 不适用于 参数(GoogleEvent)

GoogleEvent 的声明如下:

public class GoogleEvent implements IEvent {...}

我知道在 Java 中使用泛型有一些棘手的部分,因此使用通配符,但我似乎无法弄清楚这一点。

谢谢。

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);
}

This return the following compilation error for the "returnedEvents.add(new GoogleEvent()); line of code":

The method add(capture#1-of ? extends IEvent) in the type
ArrayList is not applicable for the
arguments (GoogleEvent)

The declaration of the class GoogleEvent is as follows:

public class GoogleEvent implements IEvent {...}

I know there are some tricky parts using generics in Java, thus the wild-cards but I can't seem to figure this out.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无戏配角 2024-12-17 21:16:36

你为什么不写:

public List<IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
    List<IEvent> returnedEvents = new ArrayList<IEvent>();
    returnedEvents.add(new GoogleEvent());
    return returnedEvents;
}

Why don't you write:

public List<IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
    List<IEvent> returnedEvents = new ArrayList<IEvent>();
    returnedEvents.add(new GoogleEvent());
    return returnedEvents;
}
_蜘蛛 2024-12-17 21:16:36

您不需要使用 吗?扩展 IEvent 因为仅使用 IEvent,Java 将动态绑定 GoogleEvent 类。这就是多态性。

You don't need to use ? extends IEvent because by using only IEvent, Java will dynamically bind the GoogleEvent class. It's polymorphism.

忆伤 2024-12-17 21:16:36

解决方案:

ArrayList<IEvent> returnedEvents = new ArrayList<IEvent>();

错误的原因是编译器正在尝试执行 捕获转化

在您的情况下,returnedEvents捕获了一些扩展IEvent未知(即扩展/实现IEvent的任何内容),并且您'重新将其分配给参数化类型 GoogleEvent)。

编译器发现,returnedEvents.add(? extends IEvent) 未使用 returnedEvents.add(GoogleEvent) 签名进行验证,因为在 返回事件

The solution:

ArrayList<IEvent> returnedEvents = new ArrayList<IEvent>();

The reason of your error is because the compiler is trying to do capture conversion.

In your case returnedEvents captures some unknown that extends IEvent (i.e. anything that extends/implements IEvent) and you're assigning it to a parameterized type GoogleEvent).

The compiler sees, returnedEvents.add(? extends IEvent) which doesn't validate with a signature of returnedEvents.add(GoogleEvent) as the capture placeholder is set on returnedEvents.

£烟消云散 2024-12-17 21:16:36

这是不允许的。 returnedEvents 包含 GoogleEvent 对象列表,并且不得接受非 GoogleEvent 对象的对象。

尽管在您的示例中您碰巧传递了 GoogleEvent,但您是通过调用接受任何实现 IEventadd 版本来实现这一目的的。根本不允许调用该 add 方法,因为它可能会导致列表存储 GoogleEvent 以外的内容。

Java 通配符“编辑掉”会以这种方式违反规则的方法。

如果您需要返回通配符列表类型,ArrayList 就可以满足它。

注意这是一个完整的源文件,编译不会出错:

import java.util.*;

interface IEvent { }

class GoogleEvent implements IEvent { }

public class Foo {
    public ArrayList<? extends IEvent> getEventsByDateRange()  {
        ArrayList<GoogleEvent> returnedEvents = new ArrayList<GoogleEvent>();
        returnedEvents.add(new GoogleEvent());
        return (returnedEvents);
    }
}

This is not allowed. returnedEvents contains a list of GoogleEvent objects and must not be allowed to accept objects that are not GoogleEvent objects.

Although in your example you happen to be passing a GoogleEvent, you are doing so by calling a version of add that accepts anything that implements IEvent. That add method simply isn't allowed to be called, because it could result in the list storing things other than GoogleEvent.

Java wildcards "edit out" methods that would break the rules in this way.

If you need to return that wildcarded list type, an ArrayList<GoogleEvent> satisfies it fine.

Note this is a complete source file that compiles without error:

import java.util.*;

interface IEvent { }

class GoogleEvent implements IEvent { }

public class Foo {
    public ArrayList<? extends IEvent> getEventsByDateRange()  {
        ArrayList<GoogleEvent> returnedEvents = new ArrayList<GoogleEvent>();
        returnedEvents.add(new GoogleEvent());
        return (returnedEvents);
    }
}
爱给你人给你 2024-12-17 21:16:36
public ArrayList<IEvent> getEventsByDateRange(DateTime minStartTime,
        DateTime minEndTime)
{
    ArrayList<IEvent> returnedEvents = new ArrayList<IEvent>();
    returnedEvents.add(new GoogleEvent());
    return (returnedEvents);
}
public ArrayList<IEvent> getEventsByDateRange(DateTime minStartTime,
        DateTime minEndTime)
{
    ArrayList<IEvent> returnedEvents = new ArrayList<IEvent>();
    returnedEvents.add(new GoogleEvent());
    return (returnedEvents);
}
青柠芒果 2024-12-17 21:16:36

您无法写入使用 声明的变量吗?扩展...。仅当使用 时才允许写入?超级...

这可以通过以下示例进行解释:

List<GoogleEvent> googleEvents = new ArrayList<GoogleEvent>();
ArrayList<? extends IEvent> returnedEvents = googleEvents;

// Would be OK
returnedEvents.add(new GooleEvent());

// Would be OK to the definition of returnedEvents, 
// but breaks the googleEvents.
returnedEvents.add(new OtherEvent());

最简单的解决方案是将 returnedEvents 声明为 List

You cannot writer to a variable which is declared with ? extends .... Writing is only allowed when using ? super ....

This can be explained by the following example:

List<GoogleEvent> googleEvents = new ArrayList<GoogleEvent>();
ArrayList<? extends IEvent> returnedEvents = googleEvents;

// Would be OK
returnedEvents.add(new GooleEvent());

// Would be OK to the definition of returnedEvents, 
// but breaks the googleEvents.
returnedEvents.add(new OtherEvent());

The short solution for you is to declare returnedEvents as List<GoogleEvent>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文