Java 如何在接口实现中使用泛型构造函数?
问题可能不清楚,我会在下面告诉你我想要什么 我有这样一个通用列表界面:Java 如何在接口实现中使用泛型构造函数?,java,data-structures,Java,Data Structures,问题可能不清楚,我会在下面告诉你我想要什么 我有这样一个通用列表界面: public interface List<Type> { void add(Type t); Type get(int index); void clear(); Type remove(int index); } 公共接口列表{ 无效添加(t型); 类型get(int-index); 无效清除(); 删除类型(int索引); } 并用一个示例进行了实现,因为它太长了: @O
public interface List<Type> {
void add(Type t);
Type get(int index);
void clear();
Type remove(int index);
}
公共接口列表{
无效添加(t型);
类型get(int-index);
无效清除();
删除类型(int索引);
}
并用一个示例进行了实现,因为它太长了:
@Override
public abstract class DoublyLinkedList<Type> implements List<Type> {
public void add(Type t) {
// create new node
Node<Type> newNode=new Node<>(t, null,null);
newNode.data = t;
newNode.previous = tail;
newNode.next = null;
// if list is empty
if (tail == null)
{
head = newNode;
}
else
{
tail.next = newNode;
}
tail = newNode;
}
}
@覆盖
公共抽象类DoublyLinkedList实现列表{
公共无效添加(t型){
//创建新节点
Node newNode=新节点(t,null,null);
newNode.data=t;
newNode.previous=尾部;
newNode.next=null;
//如果列表为空
if(tail==null)
{
头=新节点;
}
其他的
{
tail.next=newNode;
}
tail=newNode;
}
}
并且有一个构造器:
public abstract class Node<Type> {
protected Type data;
protected Node<Type> next;
protected Node<Type> previous;
public Node(Type data, Node<Type> next,Node<Type> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}
public Type getData() {
return data;
}
public Node<Type> getNext() {
return next;
}
public Node<Type> getPrevious() {
return previous;
}
}
public interface SortedList<Type extends Comparable<? super Type>>
公共抽象类节点{
受保护类型数据;
受保护节点下一步;
前一个受保护的节点;
公共节点(类型数据、下一个节点、上一个节点){
这个数据=数据;
this.next=next;
this.previous=先前;
}
公共类型getData(){
返回数据;
}
公共节点getNext(){
下一步返回;
}
公共节点getPrevious(){
返回上一个;
}
}
它已经给出了我知道的错误,但我以前看到过在接口实现中使用构造函数的类似情况:
public abstract class Node<Type> {
protected Type data;
protected Node<Type> next;
protected Node<Type> previous;
public Node(Type data, Node<Type> next,Node<Type> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}
public Type getData() {
return data;
}
public Node<Type> getNext() {
return next;
}
public Node<Type> getPrevious() {
return previous;
}
}
public interface SortedList<Type extends Comparable<? super Type>>
public interface SortedList我唯一能找到的是,您正在绑定创建一个Node实例,但Node是抽象的。只需从节点中删除抽象即可帮助编译代码
public class Node<Type> {
protected Type data;
protected Node<Type> next;
protected Node<Type> previous;
public Node(Type data, Node<Type> next,Node<Type> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}
public Type getData() {
return data;
}
public Node<Type> getNext() {
return next;
}
public Node<Type> getPrevious() {
return previous;
}
}
公共类节点{
受保护类型数据;
受保护节点下一步;
前一个受保护的节点;
公共节点(类型数据、下一个节点、上一个节点){
这个数据=数据;
this.next=next;
this.previous=先前;
}
公共类型getData(){
返回数据;
}
公共节点getNext(){
下一步返回;
}
公共节点getPrevious(){
返回上一个;
}
}
您的节点类是抽象的,因此无法实例化它。如果将其更改为具体类,错误将消失。@Eran然后给出类型为DoublyLinkedList的add(List,Type)方法必须重写或实现方法中的超类型方法(error)。也许编译器认为DoublyLinkedList是在实现java.util.List而不是自定义列表接口。尝试重命名列表界面。