不兼容的类型对象无法转换为 t,其中 t 是类型变量

Fil*_*ová 1 java types

我得到了不兼容的类型:对象无法转换为 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;
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*hev 5

必须是Node<T> head。您忘记添加类型参数。