[英]Calling constructor of extended class
I have this 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);
}
the next class extends the previous class and this is its constructor: 下一个类扩展了上一个类,这是它的构造函数:
public MyAdapter(Activity context, List<MyEntity> data)
{
super(context, R.layout.listview_row, data);
....
}
(MyEntity class implements IFilterableEntity <String>) (MyEntity类实现IFilterableEntity <String>)
problem is that I got error 问题是我出错了
The constructor `BaseFilterableArrayAdapter<String>(Activity, int, List<MyEntity>)` is undefined
How can I call the constructor of BaseFilterableArrayAdapter from MyAdapter ? 如何从MyAdapter调用BaseFilterableArrayAdapter的构造函数 ?
Generics are invariant so List<MyEntity>
is not the same type as List<IFilterableEntity<T>
. 泛型是不变的,因此
List<MyEntity>
与List<IFilterableEntity<T>
。 You could make the super class itself generic and make the sub class contain a <MyEntity<T>
generic type argument 您可以使超类本身具有通用性,并使子类包含
<MyEntity<T>
通用类型参数
public class BaseFilterableArrayAdapter<T> extends ArrayAdapter<T>
implements SectionIndexer{
public BaseFilterableArrayAdapter(Activity context, int idRowTemplate, List<T> data) {
super(context, idRowTemplate, data);
...
}
}
It's a known Java pain-in-the-ass. 这是众所周知的Java难题。 Look at the following code:
看下面的代码:
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>();
}
}
It won't compile: 它不会编译:
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)
Unfortunatelly you have to convert it manually before passing to superconstructor. 不幸的是,在传递给超构造函数之前,您必须手动对其进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.