2014-04-23 27 views
0

第一個問題已修復here但它附帶了另一個,我首選創建一個新線程。自定義列表<T extends A>類型不匹配

我有一個CustomList<T extends A>將與Class A1Class A2一起使用,兩個延伸Class A

我有一個Class B包含CustomList S:

public Class B 
{ 
    CustomList<A1> listA1 = new CustomList<A1>(); 
    public CustomList<A1> getListA1(){ return listA1;} 
} 

每當我試着做

for(A1 obj : myBClass.getListA1()) 
{ 
    /*Do something*/ 
} 

我得到了Type mismatch : cannot convert from element type A to A1.我不明白這一點,因爲getListA1()返回CustomList<A1>。我認爲這可能是因爲CustomList<T extends A>

回答

6

第一:

class CustomList<T extends A> extends ArrayList<T> 

二:

class A { 
    public boolean getCustomBoolean() { 
     return true; 
    } 
} 

三:

class A1 extends A { 
} 

結果:

CustomList<A1> customList = new CustomList<A1>(); 
customList.add(new A1()); 

for (A1 obj: customList) { 
    System.out.println(obj.getCustomBoolean()); 
} 

打印:true

+0

這正是我得到的。我只需要清理該項目,現在它運行良好。 –

+0

非常好!感謝您接受我的回答。祝你好運! – DmitryKanunnikoff

+1

感謝您回答我的兩個問題;) –

相關問題