簡體   English   中英

類型X中的方法X不適用於參數(int)

[英]The method X in the type X is not applicable for the arguments (int)

嘗試使用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 

如果我將泛型類擴展到另一個子泛型類,並將該子泛型類擴展到包含主函數的類,則它將起作用。

我在這里想念什么? 由於該類是通用類,因此它應該可以工作,在谷歌搜索了很多之后找不到任何答案

編輯:對不起,昨天發布這個問題時,我很疲倦,我很抱歉。 我已對問題進行了編輯,以使其更清楚。

Edit2:修復了public class CDLListTest<T> extends CDLList<T>應該是public class CDLListTest<T> extends CDLList

看來您已經編寫了一個名為insertAfter(T value) (尚未顯示給我們)。 現在,在處理CDLListTest<T>時要引用它-其中T可以是任何類或接口。 因此,當您調用insertAfter ,您傳遞的值必須為T 但是,您將其傳遞為int而不是T

更改對insertAfter的調用以傳遞T ,或者更改insertAfter方法的簽名,以使其參數的類型為int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM