小编Fil*_*ová的帖子

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

我得到了不兼容的类型:对象无法转换为 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)

java types

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

types ×1