如何在Kotlin中声明包含泛型类型字段的类?
在Kotlin我有一个数据类.
data class APIResponse<out T>(val status: String,val code: Int,val message: String,val data: T?) 我想声明另一个类来包含这个: class APIError(message: String,response: APIResponse) : Exception(message) {} 但是Kotlin给出错误:com.mypackagename中定义的类APIResponse需要一个类型参数 在Java中我可以这样做: class APIError extends Exception { APIResponse response; public APIError(String message,APIResponse response) { super(message); this.response = response; } } 如何将代码转换为Kotlin? 解决方法
Java中的内容是原始类型.在
star-projections节中,Kotlin文档说:
他们描述了他们的用例:
因此,您的APIError类变为: class APIError(message: String,val response: APIResponse<*>) : Exception(message) {} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |