当我尝试以下代码时,我正在为我的项目使用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),其中说:
答案 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