有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在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中定义的类APIResponse需要一个类型参数。mypackagename

在Java中,我可以做到:

class APIError extends Exception {

    APIResponse response;

    public APIError(String message, APIResponse response) {
        super(message);
        this.response = response;
    }
}

如何将代码转换为Kotlin


共 (0) 个答案