代码之家  ›  专栏  ›  技术社区  ›  ant2009

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

  •  7
  • ant2009  · 技术社区  · 8 年前

    我有以下接口,映射并接受为参数a 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);
    
    1 回复  |  直到 8 年前
        1
  •  7
  •   Derek    8 年前

    既然你想要一个 in A型和A型 out 键入您需要声明的内容,如下所示:

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

    那你的代码就行了