我收到不兼容的类型:对象无法转换为 T,其中 T 是类型变量:T 扩展了类 Stack 中声明的对象。
你能帮忙吗,我不知道为什么会这样,方法 pop() 和 getData() 是同一类型 T...
这是缩短的代码。
public class Stack<T> {
Node head;
public T pop() {
return head.getData(); //the error is on this line
}
private class Node <T> {
private final T data;
private Node next;
private Node(T data, Node next) {
this.data=data;
this.next=next;
}
private T getData() {
return data;
}
}
}
最佳答案
必须是Node<T> head
。您忘记添加类型参数。
关于java - 不兼容类型对象无法转换为 t,其中 t 是类型变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309727/