gpt4 book ai didi

java - 通用 setter 方法不兼容类型错误

转载 作者:行者123 更新时间:2023-11-30 02:01:19 28 4
gpt4 key购买 nike

我是泛型的新手,我正在尝试使用泛型类 SinglyLinkedNode 来实现链表。但是,当我运行 setData() 和 setNext() 方法时,出现以下错误:

.\singlyLinkedList\SinglyLinkedNode.java:13: error: incompatible types: T#1 cannot be 
converted to T#2
this.data = d;
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>setData(T#1)
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedNode.java:21: error: incompatible types:
SinglyLinkedNode<T#1> cannot be converted to SinglyLinkedNode<T#2>
this.next = n;
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>setNext(SinglyLinkedNode<T#1>)
T#2 extends Object declared in class SinglyLinkedNode

在我看来,抛出该错误是因为存在潜在的情况,其中预先存在的 this.data 或 this.next 的类型与 d 或 n 不匹配。我该如何解决这个问题?有什么方法可以用 T#2 (新数据)覆盖 T#1 (预先存在的数据)的类型吗?这是带有我的构造函数和 setter 方法的类:

public class SinglyLinkedNode<T> {
private T data;
private SinglyLinkedNode<T> next;

SinglyLinkedNode(T d) {
this.data = d;
this.next = null;
}

public <T> void setData(T d) {
this.data = d;
}

//...

public <T> void setNext(SinglyLinkedNode<T> n) {
this.next = n;
}

//...

}

感谢您提供的任何帮助。提前致谢!

编辑:感谢您的帮助!我已从方法中删除,但当我从 SinglyLinkedList 类运行 setNext() 时,仍然收到以下错误:

.\singlyLinkedList\SinglyLinkedList.java:63: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
curr.setNext() = toBeRemoved.getNext();
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedList.java:79: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
prev.setNext() = this.head;
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedList.java:82: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
prev.setNext() = curr.getNext();
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode

所以在我看来, getNext() (来自 SinglyLinkedNode)和 head (来自 SinglyLinkedList)没有返回任何值,即使它们都应该返回 SinglyLinkedNode 对象。

这是有问题的代码。我已经标记了出现错误的位置。

public class SinglyLinkedList<T> {
private int size;
private SinglyLinkedNode<T> head;
private SinglyLinkedNode<T> tail;

SinglyLinkedList() {
this.size = 0;
this.head = null;
this.tail = null;
}

//...

public SinglyLinkedNode<T> remove(int i) {
SinglyLinkedNode<T> curr = this.head;
for(int counter = 0; counter < i; counter++) {
curr = curr.getNext(); //<---------------
}
SinglyLinkedNode<T> toBeRemoved = curr.getNext();
curr.setNext() = toBeRemoved.getNext();
this.size--;
return toBeRemoved;
}

//...

public SinglyLinkedNode<T> remove(T d) {
if(this.head == null) {
return null;
}
if(this.head.getData() == d) {
SinglyLinkedNode<T> toBeRemoved = this.head;
this.head = this.head.getNext();
return toBeRemoved;
}
SinglyLinkedNode<T> curr = this.head;
SinglyLinkedNode<T> prev = null;
prev.setNext() = this.head; //<---------------
while(curr.getNext() != null) {
if(curr.getData() == d) {
prev.setNext() = curr.getNext(); //<---------------
return curr;
}
}
return null;
}

//...

如果有人仍然看到这篇文章,如果他们能帮助我,我将不胜感激。再次感谢!

编辑:我也修复了这个错误;这是

curr.setNext(toBeRemoved.getNext());

不是

curr.setNext() = toBeRemoved.getNext()

最佳答案

您面临的问题实际上非常简单。基本上,您需要了解泛型方法完全独立于泛型类

public <T> void setData(T d) {
this.data = d;
}

public <T> void setNext(SinglyLinkedNode<T> n) {
this.next = n;
}

当你输入 <T>在方法的修饰符中,您将其设为通用方法。泛型方法中的类型 T 是不同的,并且覆盖整个类的类型 T,这意味着您可以输入任何类型的对象,该方法将使用它作为其本地 T 值,这可以与全局 T 值完全不同输入 T。

删除<T>从你的方法修饰符将使它们成为常规的 setter 方法,并默认为类类型 T,保证与所需的类型匹配。

关于java - 通用 setter 方法不兼容类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52751503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com