java泛型 - 参数不能应用于方法

发布于 2024-12-07 19:25:07 字数 822 浏览 0 评论 0原文


我似乎无法弄清楚为什么我尝试进行的方法调用不起作用。
在问这个问题之前,我已经环顾四周了,虽然有(很多)关于类似问题的线程,但我找不到一个非常适合我的问题的线程。
我有以下代码:(

在文件 Processor.java 中:)

public interface Processor
{
    Runner<? extends Processor> getRunner();
}

(在文件 Runner.java 中:)

public interface Runner<P extends Processor>
{
    int runProcessors(Collection<P> processors);
}

(在其他一些文件中,在某种方法中:)

Collection<? extends Processor> processorsCollection = ...;
Runner<? extends Processor> runner = ...;
runner.runProcessors(processorsCollection);


IntelliJ 将最后一行标记为错误:
“Runner 中的 RunProcessors (java.util.Collection>) 无法应用于 (java.util.Collection>)”。
我不知道我所做的有什么问题,特别是因为错误消息不太清楚。

有什么建议吗?

谢谢。

I can't seem to figure out why a method call I'm trying to make doesn't work.

I've looked much around SO before asking this, and while there are (many) threads about similar problems, I couldn't find one that quite fits my problem..

I have the following code:

(in file Processor.java:)

public interface Processor
{
    Runner<? extends Processor> getRunner();
}

(in file Runner.java:)

public interface Runner<P extends Processor>
{
    int runProcessors(Collection<P> processors);
}

(in some other file, in some method:)

Collection<? extends Processor> processorsCollection = ...;
Runner<? extends Processor> runner = ...;
runner.runProcessors(processorsCollection);

IntelliJ marks the last line as an error:

"RunProcessors (java.util.Collection>) in Runner cannot be applied to (java.util.Collection>)".

I can't figure out whats wrong with what I did, especially since the error message is not quite clear..

any suggestions?

thanks.

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

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

发布评论

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

评论(2

痴梦一场 2024-12-14 19:25:07

您的收藏和跑步者都允许任何扩展处理器的东西。但是,您不能保证它们是相同的。

Collection 可能是Collection,Runner 可能是Runner

无论你有什么方法都需要输入(我忘记了确切的语法,但我相信你能找到它!)

void <T extends Processor<T>> foo() {
    Collection<T> procColl = ...
    Runner<T> runner = ...
    runner.runProc(procColl);
}

编辑:

@newAcct 提出了一个很好的观点:你需要通用化(这是一个词吗?)你的处理器。我更新了上面的代码片段以反映这一重要的更改。

public interface Processor<P extends Processor>
{
    Runner<P> getRunner();
}

public interface Runner<P extends Processor<P>>
{
    int runProcessors(Collection<P> processors);
}

Both your collection and your runner allow for anything that extend processor. But, you can't guarantee they're the same.

Collection might be Collection<Processor1> and Runner be Runner<Processor2>.

Whatever method you have that in needs to be typed (I forget the exact syntax, but I'm sure you can find it!)

void <T extends Processor<T>> foo() {
    Collection<T> procColl = ...
    Runner<T> runner = ...
    runner.runProc(procColl);
}

Edit:

@newAcct makes an excellent point: you need to genericize (is that a word?) your Processor. I've updated my code snippet above as to reflect this important change.

public interface Processor<P extends Processor>
{
    Runner<P> getRunner();
}

public interface Runner<P extends Processor<P>>
{
    int runProcessors(Collection<P> processors);
}
旧情别恋 2024-12-14 19:25:07

您没有明确说明您的情况,也没有向我们展示任何方法的代码或如何获取对象的代码,因此我们并不真正知道您要做什么。

您的代码不是类型安全的。正如 @glowcoder 提到的,无法知道 Collection 的参数与 Runner 的参数相同。如果您相信它们确实相同,那么这是基于您没有向我们展示的代码(即“...”中发生了什么?)

您已经编写了Processor 的 getRunner() 方法,其返回类型带有通配符参数。这表示运行时它将返回一个 Runner ,其中包含一个由它确定但我们不知道的神秘参数。这没有多大意义,并且可能不是您想要的。

另外,根据您正在执行的操作,runProcessors 方法可能采用不太严格的限制。例如,也许 甚至 <? extends Processor>(如果不需要修改集合)。

You have not made your situation clear and you're not showing us any of the code of the methods or of how you get the objects, so we don't really know what you're trying to do.

Your code is not type-safe. As @glowcoder mentioned, there is no way of knowing that the parameter of Collection is the same as the parameter of Runner. If you believe they are indeed the same, then that is based on code that you're not showing us (i.e. what happens in "..."?)

You have written Processor's getRunner() method with a return type that has a wildcard parameter. This says when run it will return a Runner with a mysterious parameter that it determines and we don't know. This doesn't make much sense and is probably not what you wanted.

Also depending on what you are doing, the runProcessors method could possibly take a less strict bound. For example, perhaps <? extends P> or even <? extends Processor> if you don't need to modify the collection.

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