當嘗試使用int參數調用泛型類的方法時出現以下錯誤。類型X中的方法X不適用於參數(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
它的工作原理,如果我泛型類擴展到另一個子類泛型類並擴展子泛型類s到包含主要功能的類。
我在這裏錯過了什麼?它應該工作,因爲類是通用的,谷歌google搜索後很多找不到答案
編輯:對不起,昨天當我發佈這個問題,我很抱歉,我很遺憾。我編輯了這個問題以使其更清楚。
EDIT2:固定它public class CDLListTest<T> extends CDLList<T>
應該是public class CDLListTest<T> extends CDLList
請貼出相關代碼;你沒有在任何地方定義任何'insertAfter'。最有可能的是,你有它接受'T',但是,就像錯誤說的那樣,你正試圖向它傳遞一個'int'。 – chrylis
Java基元不被視爲對象。 –
對於所有「T」,數字「11」不是「T」型。 *當然*它會給你一個錯誤。 – Boann