我正在进入一个目前使用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;
}
}
}
Function
和Predicate
都有apply
种方法(它们来自Google公地):
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
那么,这里发生了什么?它是编译器标志吗?
为了它的价值,我正在运行Eclipse 3.6,并且maven正在我的OSX盒子上使用Java 1.6构建:
Version: Helios Release
Build id: 20100617-1415
javac -version
javac 1.6.0_20
答案 0 :(得分:2)
这里的问题是在编译时删除了删除(尖括号之间的所有内容),使两个 apply 方法的参数相同。返回类型不同,但Java中的返回类型is not part of the method's signature。
Sun的JDK和Eclipse高达3.5并没有抱怨这种歧义(我认为他们确实看到了返回类型),但Eclipse 3.6 fixed that bug。
它应该在Eclipse 3.5中运行,但它可能只是时间问题,直到其他JDK也会修复它,所以我建议明确地实现这些方法或重命名其中一个。
答案 1 :(得分:0)
可能存在一个潜在的问题,你应该解决这个问题,因为邮件不是为了它而发出的。但是我不太了解泛型来解决它。
可能涉及的其他内容是java编译器版本。检查在Eclipse中设置哪个编译器版本级别,以及在Maven中调用哪个版本级别;他们可能会有所不同。
答案 2 :(得分:0)