使用泛型类型设计类和接口

ant*_*009 7 generics interface kotlin

我有以下接口映射并接受作为参数a InsectTypesEntity并返回a的对象InsectDataModel和另一个返回a的对象List<InsectDataModel>

我试图用泛型做这个,因为我想练习这个.

interface InsectInteractorMapper<T> {
    fun map(insectTypesEntity: T): T
    fun map(cursor: Cursor): List<T>
}
Run Code Online (Sandbox Code Playgroud)

如果没有泛型,它将是这样的:

interface InsectInteractorMapper<InsectTypesEntity> {
    fun map(insectTypesEntity: InsectTypesEntity): InsectDataModel
    fun map(cursor: Cursor): List<InsectDataModel>
}
Run Code Online (Sandbox Code Playgroud)

我试图让我的类使用接口的通用版本实现这一点,但是,我得到许多相关的错误:

1) Return type is 'insectDataModel' which is not a subtype of overridden
public abstract fun map(insectTypesEntity: InsectTypesEntity): InsectTypeEntity defined in InsectInteractorMapper

2) Return type is 'List<InsectDataModel>' which is not a subtype of overridden
public abstract fun map(cursor: Cursor): List<InsectTypesEntity> defined in InsectInteractorMapper
Run Code Online (Sandbox Code Playgroud)

实现接口的类

class InsectInteractorMapperImp: InsectInteractorMapper<InsectTypesEntity> {
    override fun map(insectTypesEntity: InsectTypesEntity): InsectDataModel {

        return InsectDataModel(
                insectTypesEntity.friendlyName,
                insectTypesEntity.scientificName,
                insectTypesEntity.classification,
                insectTypesEntity.imageAsset,
                insectTypesEntity.dangerLevel)
    }

    override fun map(cursor: Cursor): List<InsectDataModel> {
        val insectDataModelList: MutableList<InsectDataModel> = mutableListOf()

        cursor.moveToFirst()
        while(cursor.moveToNext()) {
            InsectDataModel().let {
                it.friendlyName = cursor.getString(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_FRIENDLY_NAME))
                it.scientificName = cursor.getString(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_SCIENTIFIC_NAME))
                it.dangerLevel = cursor.getInt(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_DANGER_LEVEL))

                insectDataModelList.add(it)
            }
        }

        cursor.close()
        return insectDataModelList.toList()
    }
}
Run Code Online (Sandbox Code Playgroud)

什么是使泛型正常工作的最佳方法?

非常感谢任何建议,

==== UPDATE用于输入/输出方差的修改接口:

interface InsectInteractorMapper<in E, out M> {
    fun map(insectTypesEntity: E): M
    fun map(cursor: Cursor): List<M>
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用界面时,我收到警告:

unchecked assignment java.util.List to java.util.List<InsectDataModel> Reason insectDataModelMapper has raw type so result of map will be erased 
Run Code Online (Sandbox Code Playgroud)

当我像这样使用它时:

insectInteractorMapper = new InsectInteractorMapperImp();
insectDataModelList = insectInteractorMapper.map(cursor);
Run Code Online (Sandbox Code Playgroud)

Der*_*rek 7

既然你想要一个in类型和一个out类型,你需要声明这样的类型:

interface InsectInteractorMapper<in T1, out T2> {
    fun map(insectTypesEntity: T1): T2
    fun map(cursor: Cursor): List<T2>
}
Run Code Online (Sandbox Code Playgroud)

然后你的代码将工作