我有以下代码:
public class Pair< T, U > {
public T first;
public U second;
}
public class Test {
public int method( Pair< Integer, Integer > pair ) {
return 0;
}
public double method( Pair< Double, Double > pair ) {
return 1.0;
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上编译和工作就像人们期望的那样.但是如果返回类型相同,则不会编译,预期的"名称冲突:方法(对)和方法(对)具有相同的擦除"
鉴于返回类型不是方法签名的一部分,这种重载怎么可能?
我在Java 6中有一个正确编译的接口:
public interface IMultiMap<K, V> extends Map<K, Set<V>> {
public int valueSize();
public boolean put(K key, V value);
public void clear(Object key);
public boolean isEmpty(Object key);
}
Run Code Online (Sandbox Code Playgroud)
但在Java 7中,此接口无法编译.我得到一个编译错误boolean put(K, V),它具有相同的擦除V put(K, V).编译器的完整错误:
error: name clash: put(K#1,V#1) in IMultiMap and put(K#2,V#2) in Map have the same erasure, yet neither overrides the other
public boolean put(K key, V value);
where K#1,V#1,K#2,V#2 are type-variables:
K#1 extends Object declared in interface IMultiMap
V#1 extends Object declared in interface …Run Code Online (Sandbox Code Playgroud)