繁体   English   中英

调用扩展类的构造函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM