Kotlin泛型类型类作为函数参数

时间:2020-07-14 08:49:10

标签: kotlin generics

如何将参数传递给函数,然后将其用作通用类型?

 private fun createRequest(data: Data, class: Class<out UploaderTask>): OneTimeWorkRequest {
    return OneTimeWorkRequestBuilder<class>().build()
 }

1 个答案:

答案 0 :(得分:0)

我找到了答案

private inline fun <reified T : UploaderTask>createRequest(data: Data): OneTimeWorkRequest {
    return OneTimeWorkRequestBuilder<T>()
        .setInputData(data)
        .build()
}