我的任务是完成一个 append 方法,该方法将返回一个列表接口(interface)。列表接口(interface)是一个给定的类,它基本上只有简单的方法,例如 size()、append() 等。在一个新类中,我必须使用链表编写一个追加方法,并返回一个列表接口(interface)。问题出在我的返回上,因为它说 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
}
最佳答案
您需要返回一个实现 ListInterface 接口(interface)的类型的对象。看起来您想在方法末尾返回“this”。我猜你的类实现了 ListInterface?
关于java - 我该如何返回这个接口(interface)呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966702/