How to declare a class containing generic type fields in kotlin?
•
Java
I have a data class in kotlin
data class APIResponse<out T>(val status: String,val code: Int,val message: String,val data: T?)
I want to declare another class to contain this:
class APIError(message: String,response: APIResponse) : Exception(message) {}
But kotlin gave the error: com The class apiresponse defined in mypackagename requires a type parameter
In Java, I can do this:
class APIError extends Exception { APIResponse response; public APIError(String message,APIResponse response) { super(message); this.response = response; } }
How to add @ r_ 404_ 2324 @ for kotlin?
Solution
Content in Java is of primitive type In the star projects section, the kotlin document says:
They describe their use cases:
Therefore, your apierror class becomes:
class APIError(message: String,val response: APIResponse<*>) : Exception(message) {}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码