代碼:問題與泛型
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