【发布时间】:2013-12-04 11:07:12
【问题描述】:
给定以下接口和类:
public interface Interface<T> {
List<T> get(List<List<Object>> keys);
}
public class Cls implements Interface {
@Override
public List get(List<List<Object>> keys) {
return Collections.emptyList();
}
}
导致编译错误:
TypeTest.java:[9,11] error: Cls is not abstract and does not override abstract method get(List) in Interface TypeTest.java:[12,20] error: name clash: get(List<List<Object>>) in Cls and get(List<List<Object>>) in Interface have the same erasure, yet neither overrides the other
但这有效:
public interface Interface<T> {
List<T> get();
}
public class Cls implements Interface {
@Override
public List get() {
return Collections.emptyList();
}
}
就像这样:
public interface Interface<T> {
List<T> get(List<List<Object>> keys);
}
public class Cls implements Interface {
@Override
public List get(List keys) {
return Collections.emptyList();
}
}
我不明白这种行为。类型擦除只适用于类型参数T,不是吗?为什么错过T 会影响无关的List<List<Object>> keys 参数?
【问题讨论】:
-
你为什么不实现
Interface<T>? -
我正在使用的框架的限制。我对尝试理解奇怪的行为更感兴趣;我已经解决了编译错误。
-
我的猜测是 List*> 是 T 所以实现者应该得到的是 List
-
T和List<List<Object>>是完全独立的。这是多键查找界面的简化版。
标签: java generics type-erasure raw-types