[英]Calling constructor of extended class
我有這個課:
public class BaseFilterableArrayAdapter<T> extends ArrayAdapter< IFilterableEntity<T> > implements SectionIndexer
{
public BaseFilterableArrayAdapter(Activity context,int id_row_template, List<IFilterableEntity<T>> data)
{
super(context, id_row_template, data);
}
下一個類擴展了上一個類,這是它的構造函數:
public MyAdapter(Activity context, List<MyEntity> data)
{
super(context, R.layout.listview_row, data);
....
}
(MyEntity類實現IFilterableEntity <String>)
問題是我出錯了
The constructor `BaseFilterableArrayAdapter<String>(Activity, int, List<MyEntity>)` is undefined
如何從MyAdapter調用BaseFilterableArrayAdapter的構造函數 ?
泛型是不變的,因此List<MyEntity>
與List<IFilterableEntity<T>
。 您可以使超類本身具有通用性,並使子類包含<MyEntity<T>
通用類型參數
public class BaseFilterableArrayAdapter<T> extends ArrayAdapter<T>
implements SectionIndexer{
public BaseFilterableArrayAdapter(Activity context, int idRowTemplate, List<T> data) {
super(context, idRowTemplate, data);
...
}
}
這是眾所周知的Java難題。 看下面的代碼:
import java.awt.List;
import java.util.ArrayList;
class Animal {
}
class Cat extends Animal {
}
public class TemplatedList {
public static void main(String[] args) {
ArrayList<Animal> animals = new ArrayList<Cat>();
}
}
它不會編譯:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from ArrayList<Cat> to ArrayList<Animal>
at TemplatedList.main(TemplatedList.java:14)
不幸的是,在傳遞給超構造函數之前,您必須手動對其進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.