gpt4 book ai didi

java - 为什么这在 Maven 下编译正常,但在 Eclipse 中给我错误?

转载 作者:行者123 更新时间:2023-11-30 09:54:27 27 4
gpt4 key购买 nike

我正在进入一个开源项目,目前使用 Maven2 构建。我将项目导入到 Eclipse 中,这段代码给我带来了问题:

public static enum HierarchyType implements Function<MonetarySummary, SumOfMoney>, Predicate<Txaction> {
EARNINGS {
@Override
public SumOfMoney apply(MonetarySummary summary) {
return summary.getEarnings();
}

@Override
public boolean apply(Txaction txaction) {
return txaction.getAmount().signum() > 0;
}
},
SPENDING {
@Override
public SumOfMoney apply(MonetarySummary summary) {
return summary.getSpending();
}

@Override
public boolean apply(Txaction txaction) {
return txaction.getAmount().signum() < 0;
}
},
NET {
@Override
public SumOfMoney apply(MonetarySummary summary) {
return summary.getNet();
}

@Override
public boolean apply(Txaction txaction) {
return true;
}
}
}

FunctionPredicate 都有 apply 方法(它们来自 Google commons):

public interface Function<F, T> {
T apply(@Nullable F from);
...
}

public interface Predicate<T> {
boolean apply(@Nullable T input);
}

这是我得到的错误,仅在 Eclipse 中:

Description Resource    Path    Location    Type
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it

那么,这里发生了什么?它是编译器标志吗?

就其值(value)而言,我正在运行 Eclipse 3.6,而 maven 在我的 OSX 机器上使用 Java 1.6 进行构建:

Version: Helios Release
Build id: 20100617-1415

javac -version
javac 1.6.0_20

最佳答案

这里的问题是删除(尖括号之间的所有内容)在编译时被删除,使两个 apply 方法的参数相同。返回类型不同,但返回类型 is not part of the method's signature在 Java 中。

Sun 的 JDK 和 Eclipse 到 3.5 不会提示这种歧义(我认为它们实际上会查看返回类型),但 Eclipse 3.6 fixed that bug .

它应该在 Eclipse 3.5 中工作,但其他 JDK 也将修复此问题可能只是时间问题,因此我建议显式实现这些方法或重命名其中一个方法。

关于java - 为什么这在 Maven 下编译正常,但在 Eclipse 中给我错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265239/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com