[英]Generic interface implementation not working
我的界面是:
public interface SalesForceRecordService<T extends SalesForceRecord> {
T update(T record) throws IOException;
}
我的實現類是:
public class SalesForceRecordServiceImpl<T extends SalesforceRecordImpl> implements SalesforceRecordService {
@Override
public T update(T record) throws IOException {
return null;
}
}
@Override
給出錯誤,方法不會從其超類覆蓋方法。
我無法理解問題所在。
您必須將類型傳遞給超類,因為它需要一個。 如果您希望SalesForceRecordServiceImpl
也像您發布的一樣通用,則將T
傳遞給SalesForceRecordService
。
嘗試這個:
public class SalesForceRecordServiceImpl<T extends SalesForceRecordImpl>
implements SalesForceRecordService<T> {
@Override
public T update(T record) throws IOException {
return null;
}
}
同樣以其名稱判斷, SalesForceRecordImpl
在我看來像是一個具體的實現,所以也許您只想要這樣:
public class SalesForceRecordServiceImpl
implements SalesForceRecordService<SalesForceRecordImpl> {
@Override
public SalesForceRecordImpl update(SalesForceRecordImpl record)
throws IOException {
return null;
}
}
首先,在實現類上,還應該將類型模板傳遞到接口描述中。 否則,編譯器無法檢查SalesForceRecordServiceImpl
上提供的T是否與SalesforceRecordService
兼容。
public class SalesForceRecordServiceImpl<T extends SalesforceRecordImpl>
implements SalesforceRecordService<T> {
...
(我假設SalesforceRecordImpl是SalesforceRecord的子類型)
其次,強制方法使用@Overrides
注釋實現接口,該方法僅從Java 6起可用,因此,如果您使用的是Java 5,則會出現編譯器錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.