首页 文章

类型X中的方法X不适用于参数(int)

提问于
浏览
0

尝试使用int参数调用泛型类的方法时,我收到以下错误 .

The method insertAfter(T) in the type CDLList<T>.Writer is not applicable for the arguments (int)

通用类代码是

public class CDLList<T> {
public Element Head;
static int count;
public CDLList(T v)
{
    Head = new Element(v);
}
public class Element 
{
    public T data;
    public Element next;
    public Element prev;

    Element(T v)
    {
        data = v;
        prev=null;
        next=null;
        count++;
    }
    public T value() 
    {
        return data;
    }
}
public Element head() 
{
    return Head;
}
public Cursor reader(Element from) 
{
    Cursor CurrCursor=new Cursor(from);
    return CurrCursor;
}
public class Cursor 
{
    public Element current;
    Cursor(Element v)
    {
        current=v;
    }
    public Element current() 
    {
        T temp;
        temp = current.value();
        System.out.println(temp);
        return current;
    }
    public void previous() 
    {
        current = current.prev;
    }
    public void next()
    {
        current = current.next;
    }
    public Writer writer()
    {
        Writer nwriter = new Writer( current);

        return nwriter;

    }
}



public class Writer
{
    public Element current;
    Writer(Element temp)
    {
        current=temp;
    }
    public boolean delete()
    {
        Element Td1,Td2;
        Td1 = current.prev;
        Td2 = current.next;
        current=null;
        Td1.next = Td2;
        Td2.prev = Td1;
        return true;

    }
    public boolean insertBefore(T val)
    {

        Element t = new Element(val);
        Element t2 = current.prev;
        t2.next=t;
        current.prev=t;
        t.next=current;
        t.prev=t2;      
        return true;
    }
    public boolean insertAfter(T val)
    {
        Element t = new Element(val);
        Element t1 = current.next;
        t.next=t1;
        t1.prev=t;
        current.next=t;
        t.prev=current;
        return true;

    }
}

}

实现泛型类的类是

public class CDLListTest<T> extends CDLList<T> implements Runnable {
Cursor cursor;

public CDLListTest(T v) {
    super(v);
    // TODO Auto-generated constructor stub
            Element t1= new CDLList.Element(20); 
            -----------------------
    temp.writer().insertAfter(11); -- Getting error here

如果我将泛型类扩展到另一个子泛型类并将子泛型类扩展为包含main函数的类,它就可以工作 .

我在这里错过了什么?它应该工作,因为类是通用的,经过谷歌搜索很多无法找到任何答案

编辑:对不起,我昨天发帖时已经被烧坏了,道歉 . 我编辑了这个问题以使其更清晰 .

编辑2:修正了 public class CDLListTest<T> extends CDLList<T> 应该是 public class CDLListTest<T> extends CDLList

1 回答

  • 1

    看起来你已经编写了一个名为 insertAfter(T value) 的方法(当你处理 CDLListTest<T> 时,你没有't shown us). Now, you'引用它 - 其中 T 可以是任何类或接口 . 因此,当你调用 insertAfter 时,值你传递它必须是 T . 但你传递的是 int 而不是 T .

    将您的调用更改为 insertAfter 以传递 T ,或更改 insertAfter 方法的签名,使其参数的类型为 int .

相关问题