繁体   English   中英

为什么java.lang.Object不能转成T?

[英]Why can java.lang.Object no be coverted into T?

我正在制作一个通用链接列表,我遇到了这个错误

incompatible types: java.lang.object cannot be converted into T

我不明白,因为 getData 和 data 是 T 的。

// accessor and mutator for the data component
    public T getData()
    {
        return data;
    }

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

这是错误来自的代码:

// copy constructor
    // clones the list l and sets the last element as the current
    public List(List l)
    {
        Node n = l.head;
        this.head = null;
        this.tail = null;
        this.curr = null;
        this.num_items = 0;

        while(n!= null)
        {
            this.InsertAfter(n.getData());
            n = n.getLink();
        }
    }
public List(List l)

应该读

public List(List<T> l)

编译器应该给你一个原始类型警告。 (只要您启用了 lint。请启用 lint。)

或者可能更通用,但可读性较差:

public List(List<? extends T> l)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM