【问题标题】:Designing classes and interfaces with generic types使用泛型类型设计类和接口
【发布时间】:2018-06-12 16:07:34
【问题描述】:

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

我正在尝试使用泛型来做到这一点,因为我想练习这个。

interface InsectInteractorMapper<T> {
    fun map(insectTypesEntity: T): T
    fun map(cursor: Cursor): List<T>
}

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

interface InsectInteractorMapper<InsectTypesEntity> {
    fun map(insectTypesEntity: InsectTypesEntity): InsectDataModel
    fun map(cursor: Cursor): List<InsectDataModel>
}

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

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

实现接口的类

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()
    }
}

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

非常感谢您的任何建议,

==== 更新 输入/输出差异的修改界面:

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

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

unchecked assignment java.util.List to java.util.List<InsectDataModel> Reason insectDataModelMapper has raw type so result of map will be erased 

当我这样使用它时:

insectInteractorMapper = new InsectInteractorMapperImp();
insectDataModelList = insectInteractorMapper.map(cursor);

【问题讨论】:

  • 更新后您的InsectInteractorMapperImp 声明如何?

标签: generics interface kotlin


【解决方案1】:

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

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

那么你的代码就可以工作了

【讨论】:

  • 这个答案确实有效,谢谢。但是,我收到了一个未经检查的作业警告。我已经更新了我的问题
  • @ant2009 如果列表是 insectDataModelList 是什么类型?是InsectDataModel的列表吗?
  • derek,是的,它是一个 InsectDataModel。这是声明 private List&lt;InsectDataModel&gt; insectDataModelList = Collections.emptyList(); 它在一个 java 类中。
  • @ant2009 如果你输入List&lt;InsectDataModel&gt; insectDataModelList = insectInteractorMapper.map(cursor); 你会得到同样的警告吗?警告几乎说您拥有的列表与它将返回的列表不匹配
  • @ant2009 如果知道类型也可以在函数上方添加@SuppressWarnings("unchecked") 忽略它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
相关资源
最近更新 更多