正如Oliver Charlesworth指出的那樣,在BatchExex<...>
的U
和T
比在Exec<T, U>
不同。即如果聲明BatchExec
這樣的:
public class BatchExec<T, U> implements Exec<List<T>, List<U>>
然後執行方法簽名將包含List<T>
和List<U>
:
public List<U> execute(List<T> context)
這可能會造成混亂,以便讓我們創建與其他類型參數的OtherbatchExec
:
public class OtherBatchExec<P, Q> implements Exec<List<P>, List<Q>> {
@Override
public List<Q> execute(List<P> context) {
return null;
}
}
只是爲了證明它,你可以用同樣的方式調用它們的構造函數:
Exec<List<String>, List<Integer>> exec = new BatchExec<String, Integer>();
Exec<List<String>, List<Integer>> otherExec = new OtherBatchExec<String, Integer>();
爲了便於閱讀,我也將類型參數添加到構造函數調用中。您可以使用diamond operator太:在`BatchExec
Exec<List<String>, List<Integer>> exec = new BatchExec<>();
Exec<List<String>, List<Integer>> otherExec = new OtherBatchExec<>();
的'T'和''U'是完全無關的'T'和'U'在'Exec的'。將它們想象爲方法參數 - 多個方法可以具有相同名稱的參數。 –