首页 > 解决方案 > 如何将类实例作为参数提供给 Kotlin 函数

问题描述

您好我是 Kotlin 的新手,我想知道我应该如何为 Kotlin 函数提供类实例参数

这是我的 Java 代码

Auth_REST_API_Client.GET("URL", null, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                
            }

        });

现在在 kotlin 中没有new关键字,那么我如何提供new JsonHttpRepsonseHandler()给 kotlin 函数

kotlin 函数是:

Auth_REST_API_Client.GET(
            "URL",
            null,
            JsonHttpResponseHandler() {

            }
        )

出现错误JsonHttpResponseHandler()

标签: androidandroid-studiokotlin

解决方案


要创建从某种(或多个类型)继承的匿名类的对象,请使用object关键字:

Auth_REST_API_Client.GET("URL", null, object : JsonHttpResponseHandler {

    override fun onSuccess(statusCode: Int, headers: Array<Header>, response: JSONObject) {

    }

    override fun onFailure(statusCode: Int, headers: Array<Header>, throwable: Throwable, response: JSONObject) {

    }

})

推荐阅读