Kotlin + Intelij + HtmlUnit:使用提供的参数不能调用以下任何函数

时间:2016-08-17 04:36:38

标签: kotlin htmlunit

当我尝试以下代码时,我正在为我的项目使用HtmlUnit:

webClient.webConnection = FalsifyingWebConnection(webClient) {
            fun getResponse(request: WebRequest): WebResponse {
                if(request.getUrl().getPath().toLowerCase().equals("some url i don't need ")) {
                    return createWebResponse(request, "", "application/javascript");
                }

            return super.getResponse(request);
        }
    }

错误发生在 FalsifyingWebConnection(webClient),其中说:

enter image description here

1 个答案:

答案 0 :(得分:3)

如果您正在寻找一种使用方法覆盖来实例化对象的方法,那么您需要一个对象表达式:

webClient.webConnection = object : FalsifyingWebConnection(webClient) {
    override fun getResponse(request: WebRequest): WebResponse {
        if(request.getUrl().getPath().toLowerCase().equals("some url i don't need ")) {
            return createWebResponse(request, "", "application/javascript");
        }

        return super.getResponse(request);
    }
}

https://kotlinlang.org/docs/reference/object-declarations.html