代码:问题与泛型
interface BaseIntr<T>{
void saveSwiftMsg(Collection<String> headers) throws Exception;
}
class Impl implements BaseIntr{
public void saveSwiftMsg(Collection<String> headers) throws Exception { }
}
EDITED版本的代码:
interface BaseIntr<T>{
T process(T t);
void saveSwiftMsg(Collection<String> headers) throws Exception;
}
class Impl implements BaseIntr{
public void saveSwiftMsg(Collection<String> headers) throws Exception { }
@Override
public Object process(Object t) { return t; }
}
工作环境:
Java版本:1.7.0_04,供应商:甲骨文公司
默认区域:en_IN,平台编码:的Cp1252
操作系统名称: “Windows 7的” 版本: “6.1”,拱: “AMD64”,家人: “窗口”
Java编译器投以下例外。但是如果我从BaseIntr中删除< T>,它工作正常。
javac gen\GenericsTest.java
gen\GenericsTest.java:18: error: Impl is not abstract and does not override abstract method saveSwiftMsg(Collection) in BaseIntr
class Impl implements BaseIntr{
^
gen\GenericsTest.java:20: error: name clash: saveSwiftMsg(Collection<String>) in Impl and saveSwiftMsg(Collection<String>) in BaseIntr have the same erasure, yet neither overrides the other
public void saveSwiftMsg(Collection<String> headers) throws Exception {
^
2 errors
你为什么使界面通用?你不要在任何地方使用'T' ... – assylias 2013-03-08 12:25:30
确保Collection类是相同的,以及方法名称是相同的(一个字符可以用不同的语言编写)。 编辑:这很奇怪,它不能缺少@Override? 此外,尝试使用某些参数化“实现BaseIntr”,例如执行BaseIntr
@assylias,是否使用T是强制性的,尽管没有使用,但它几乎没有任何意义。 – 2013-03-08 12:30:16