简体   繁体   English

如何在Kotlin中进行仿制药?

[英]How to do a generic in kotlin?

I have a problem where I need to pass a generic in kotlin of a certain type of class 我有一个问题,我需要在某种类型的类的kotlin中传递泛型

I have this method 我有这种方法

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

And its overriding this: 并覆盖此:

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

Yet its gives me an error. 但是它给了我一个错误。 Can you help me on how to say the ApiResponse to Expect a class that as a List inside? 您能帮我怎么说ApiResponse以Expect类作为List内部吗? ( I have reasons to do this so I cant just pass the list directly). (我有这样做的理由,所以我不能直接通过列表)。 thanks 谢谢

You can do this: 你可以这样做:

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

Then you can have an implementation of RequestType which has a List internally. 然后,您可以实现RequestType的实现,该实现在内部具有一个List

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

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