【问题标题】:Referencing overloaded Java methods in Kotlin在 Kotlin 中引用重载的 Java 方法
【发布时间】:2020-04-23 16:32:20
【问题描述】:

我想知道如何从 Kotlin 中的 Java 类中引用重载方法。

例如在接口WebClient中有三个get方法:

  HttpRequest<Buffer> get(int port, String host, String requestURI);
  HttpRequest<Buffer> get(String host, String requestURI);
  ...

在 kotlin 中,我想引用具有三个参数的那个:

val methodReference : WebClient.(port : Int, host: String?, requestUrl: String?) -> HttpRequest<Buffer> = WebClient::get

但我收到以下错误:

None of the following functions can be called with the arguments supplied: 
public abstract operator fun get(p0: Int, p1: String!, p2: String!): HttpRequest<Buffer!>! defined in io.vertx.ext.web.client.WebClient
public abstract operator fun get(p0: String!): HttpRequest<Buffer!>! defined in io.vertx.ext.web.client.WebClient
public abstract operator fun get(p0: String!, p1: String!): HttpRequest<Buffer!>! defined in io.vertx.ext.web.client.WebClient

有什么想法吗?

【问题讨论】:

  • 您是否使用与您所指的函数相同的Buffer 类(和相同的HttpRequest)类? (不同包中的同名类很容易混淆,错误信息也不一定能区分。)
  • @gidds 你是对的......程序员的盲目性。谢谢!

标签: java kotlin


【解决方案1】:

尝试像这样获取参考:

val methodReference: WebClient.(Int, String, String) -> HttpRequest<Buffer>? = WebClient::get

【讨论】:

  • 同样的问题。
  • @AndreiCiobanu 很好,它对我有用!检查imgur.com/a/fiGAYPA您遇到了哪个错误?
【解决方案2】:

也许你可以用默认值试试。

val methodReference : WebClient.(port:Int=8080, host:String='host', requestUrl:String='url') -> HttpRequest<Buffer> = WebClient::get

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多