繁体   English   中英

如何在Kotlin中进行仿制药?

[英]How to do a generic in kotlin?

我有一个问题,我需要在某种类型的类的kotlin中传递泛型

我有这种方法

 override fun fetchService(): LiveData<ApiResponse<ProductPostListing>> { // TODO mudar request type
                return service.getPosts("", "",page, per_page)
            }

并覆盖此:

@MainThread
    protected abstract fun fetchService(): LiveData<ApiResponse<RequestType>>

但是它给了我一个错误。 您能帮我怎么说ApiResponse以Expect类作为List内部吗? (我有这样做的理由,所以我不能直接通过列表)。 谢谢

你可以这样做:

@MainThread
protected abstract fun fetchService(): LiveData<ApiResponse<out RequestType>>

然后,您可以实现RequestType的实现,该实现在内部具有一个List

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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