Java通用接口方法重载

Java通用接口方法重载,java,generics,overloading,Java,Generics,Overloading,我正试图为我在大学的任务表制作一个界面。我必须为各种数据结构实现所有这些方法,所以我想实现这个接口 问题是,数据结构必须是泛型的,例如:LinearList,其中键的类型是T。现在不同的数据结构有不同的元素,例如LinearList有ListItem,就像元素和树有TreeNode一样 所以我想我用做了一个接口,其中C=ListItem,T是exinteger类型 现在我有一些重载方法,例如: insert(T key) insert(ListItem<T> item) 我可以做些

我正试图为我在大学的任务表制作一个界面。我必须为各种数据结构实现所有这些方法,所以我想实现这个接口

问题是,数据结构必须是泛型的,例如:
LinearList
,其中键的类型是T。现在不同的数据结构有不同的元素,例如
LinearList
ListItem
,就像元素和树有
TreeNode
一样

所以我想我用
做了一个接口,其中C=ListItem,T是exinteger类型

现在我有一些重载方法,例如:

insert(T key)
insert(ListItem<T> item)
我可以做些什么来启用上面解释的重载?因为在一个抽象类中,我尝试了它,它成功了

编辑: 好的,就像在讨论的评论中一样,我现在使用不同的方法名。它是其他集合用来解决问题的解决方案,如前所述,隐藏了更多的实现细节。非常感谢您的支持

package interfaces;

/**
 * Interface with all methods for the data-structs I have to learn for the exam
 *
 * <C> is the class of the Elements to be entered ex: ListItem<T>
 * <T> the type of the elements stored in the data-structs
 * 
 * @author 
 */
public interface ExamPreparation<C, T> {

    boolean insert(C item, int pos);

    boolean insert(T key, int pos);

    boolean insertAtHead(C item);

    boolean insertAtHead(T key);

    boolean insertAtTail(C item);

    boolean insertAtTail(T key);

    boolean insertSorted(C item, Comparator<T> comp);

    boolean insertSorted(T key, Comparator<T> comp);


    // ========== Remove Methods ==========

    boolean remove(T key);

    boolean removeAll(T key);


    // ========== Overwrite Methods ==========

    /**
     * takes the first appearance of oldKey and overwrites it wit
    h newKey
     *
     * @param newKey
     * @param oldKey
     * @return true if overwrited. False if oldKey is not in list
     */
    boolean overwrite(T newKey, T oldKey);

    /**
     * takes all the oldKeys and overwrites it with the newKey
     *
     * @param newKey
     * @param oldKey
     * @return returns true if at least one oldkey was found
     */
    boolean overwriteAll(T newKey, T oldKey);

    /**
     * overwrite at position
     *
     * @param newKey
     * @param pos
     * @return returns false if pos is not valid else true
     */
    boolean overwriteAt(T newKey, int pos);


    // ========== Other ==========

    boolean contains(T key); 
}
包接口;
/**
*与我必须为考试学习的数据结构的所有方法的接口
*
*是要输入的元素的类,例如:ListItem
*存储在数据结构中的元素的类型
* 
*@作者
*/
公共接口测试准备{
布尔插入(C项,int-pos);
布尔插入(T键,int-pos);
布尔插入符(C项);
布尔插入符(T键);
布尔插入附件(C项);
布尔插入附件(T键);
布尔insertSorted(C项,比较器comp);
布尔插入排序(T键,比较器comp);
//==========删除方法==========
布尔删除(T键);
布尔removeAll(T键);
//============覆盖方法==========
/**
*获取oldKey的第一个外观并用
h纽基
*
*@param newKey
*@param oldKey
*@如果覆盖,则返回true。如果列表中没有oldKey,则返回False
*/
布尔覆盖(T newKey,T oldKey);
/**
*获取所有旧密钥并用新密钥覆盖它
*
*@param newKey
*@param oldKey
*@return如果至少找到一个oldkey,则返回true
*/
布尔覆盖all(T newKey,T oldKey);
/**
*覆盖位置
*
*@param newKey
*@param pos
*@return如果pos无效则返回false,否则返回true
*/
布尔覆盖(T newKey,int pos);
//=============其他==========
布尔包含(T键);
}

声明一个新接口怎么样

interface ElementType<T> {

}
最后更改
examprepare

公共接口示例准备

公共接口示例准备
这样下面的代码编译就不会有问题了

    ListItem<String> item = null;
    ExamPreparation<ListItem<String>, String> examPreparation = null;
    boolean isInserted;
    isInserted = examPreparation.insert("12", 0);
    isInserted = examPreparation.insert(item, 0);
ListItem=null;
ExamPreparion ExamPreparion=null;
布尔输入;
isInserted=ExamPreparion.插入(“12”,0);
isInserted=ExamPreparion.insert(项目,0);

删除
C
,只需使用
T
,正如您前面解释的那样
insert(T)
insert(liselement)
非常好而且没有冲突。作为用户,我为什么想知道其他列表的标题?看看Java对集合API的实现——一个
LinkedList
只是一个
列表
。不要在公共API中公开内部实现细节。只需以不同的方式命名插入列表所有元素的方法,如
insertAll(ListItem)
。这就是标准集合所做的。Java集合具有
addAll(Collection
,它接受另一个集合并添加所有项目。您的列表可以确定用户想要添加另一个列表并在内部请求标题。再次隐藏实现细节。正如其他人指出的那样:您应该考虑是否应该有插入“项目”和“键”的选项如果你想要两个选项:为什么不相应地命名方法?
insertItem
insertKey
等等。具体一点。查看
列表
界面引起的头痛,包括
删除(T)
删除(int-index)
。对于
列表
,这会导致非常微妙的错误。命名方法
removeElement
removeByIndex
可以避免这里的许多问题…
C扩展集合
?考虑到OP所描述的内容,这为什么会有帮助?只需声明
C
,而与
T
没有任何关系将使这个接口违反了可以和不能被称为元素的内容。比如
T
是什么的元素?所以这种声明使它clear@BoristheSpider你是对的,我错了。编辑的帖子怎么样?
interface ListItem<T> extends ElementType<T> {

}

interface TreeNode<T> extends ElementType<T> {

}
public interface ExamPreparation<C,  T>
public interface ExamPreparation<C extends ElementType<T>,  T>
    ListItem<String> item = null;
    ExamPreparation<ListItem<String>, String> examPreparation = null;
    boolean isInserted;
    isInserted = examPreparation.insert("12", 0);
    isInserted = examPreparation.insert(item, 0);