Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回此接口?_Java_Append - Fatal编程技术网

Java 如何返回此接口?

Java 如何返回此接口?,java,append,Java,Append,我的任务是完成一个append方法,它将返回一个列表接口。列表接口是一个给定的类,它基本上只有简单的方法,如size()、append(),等等。在一个新的类中,我必须使用链表编写一个append方法,并返回一个列表接口。问题在于我的返回,因为它说ListInterface无法实例化。如果有帮助的话,类ListInterface只有我们必须在新类中实现的方法。以下是我的append方法: public ListInterface<T> append(T elem) { Nod

我的任务是完成一个append方法,它将返回一个列表接口。列表接口是一个给定的类,它基本上只有简单的方法,如size()、append(),等等。在一个新的类中,我必须使用链表编写一个append方法,并返回一个列表接口。问题在于我的返回,因为它说ListInterface无法实例化。如果有帮助的话,类ListInterface只有我们必须在新类中实现的方法。以下是我的append方法:

public ListInterface<T> append(T elem)
{
    Node<T> curr= new Node<T>(elem,null);
    if(tail==null)
    {
        head=curr;
    }
    else
    {
        tail.setNext(curr);
    }
    tail=curr;
    size++;
    return new ListInterface<T>(head); //need help here
}
公共列表接口附加(T元素)
{
节点电流=新节点(元素,空);
if(tail==null)
{
水头=电流;
}
其他的
{
tail.setNext(curr);
}
尾=电流;
大小++;
返回新的ListInterface(head);//此处需要帮助
}

您需要返回实现接口ListInterface的类型的对象。看起来您希望在方法末尾返回“this”。我猜你的类实现了ListInterface?

你需要创建。这个方法
append
在哪里声明的?(可能显示接口也会有一些帮助…)append在接口中声明,但我们在我实现它的新类中定义了它。那么你的新类实现了你的列表接口?在这种情况下,您已经拥有了需要返回的对象。它与调用
append
方法的对象相同。最后一行应该是
返回这个
。是的,对我来说这听起来是正确的解决方案。所以你把“elem”附加到你的列表中,然后返回你的列表。不一定。只要一个方法中定义的所有方法都已实现,该方法就可以返回所述接口。(即,
返回新迭代器(){public boolean hasNext(){}public T next(){}public void remove(){}
,尽管我不知道OP想要什么。)