【发布时间】:2018-10-09 20:41:26
【问题描述】:
我有一个名为ListInterface<T> 的接口,它具有join 方法:
public interface ListInterface<T> {
/**
* Join a new list to the original list.
* @param otherList The new list to be joined.
* @return Original list with appended part from the new list.
* @throws IllegalArgumentException.
*/
public ListInterface<T> join(ListInterface<T> otherList) throws IllegalArgumentException;
}
DoubleLinkedList 和 MyArrayList 两个类实现了这个接口。
所以在DoubleLinkedList,我需要这样写join:
public ListInterface<T> join(DoubleLinkedList<T> otherList) throws IllegalArgumentException {
(...)
}
在MyArrayList:
public ListInterface<T> join(MyArrayList<T> otherList) throws IllegalArgumentException {
(...)
}
但这是不可能的,参数的类型必须是ListInterface<T>。如果我将这些类中的方法签名更改为public ListInterface<T> join(ListInterface<T> otherList),那么我将无法再在DoubleLinkedList 或MyArrayList 上使用其他特定方法。我应该如何更改 ListInterface 中的方法签名以解决此问题?
【问题讨论】:
标签: java class generics methods interface