在Android Java中实现完成处理程序lambda。

huangapple go评论77阅读模式
英文:

Implement completion handler lambda in Android java

问题

我在Kotlin中有一个带有如下回调函数的方法,

@JvmStatic
fun getContents(callBack:(filePath:String,name:String,expiry:Int,metadata:String) -> Unit){

}

我可以在Kotlin中访问这个函数,但如何从Java类中访问它呢?

英文:

I have a method in Kotlin which has a callback function like below,

@JvmStatic
fun getContents(callBack:(filePath:String,name:String,expiry:Int,metadata:String) -> Unit){

}

I can access this function in kotlin, but how can I access this from a Java class?

答案1

得分: 0

以下是您要翻译的内容:

用例#1 - kotlin文件中的函数

例如,如果您有以下kotlin文件:Content.kt

文件的内容将如下所示:

fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
}

然后,您可以从Java中像以下这样调用它:

ContentKt.getContents((filePath, name, expiry, metadata) -> {
  return Unit.INSTANCE;
});

用例#2 - kotlin文件中的函数,更改jvm名称

如果您想要删除添加到文件名的kt或者为它指定任何其他逻辑名称,还可以指定@file:JvmName(...)

@file:JvmName("Content")

package test

fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
}

然后,您可以像这样调用它:

Content.getContents((filePath, name, expiry, metadata) -> {
    return Unit.INSTANCE;
});

用例#3 - 伴生对象中的静态函数或在对象中

考虑以下kotlin类:

class Content {
  companion object {
    @JvmStatic
    fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
    }
  }
}

或者以下对象:

object Content {
  @JvmStatic
  fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
  }
}

您将通过Java这样访问它:

Content.getContents((filePath, name, expiry, metadata) -> {
    return Unit.INSTANCE;
});
英文:

You have many ways, I'll list some of them below and it should help direct you to build it the way you want.

Use case #1 - function inside a kotlin file

If you have the following kotlin file for example: Content.kt

The content of the file will be something like:

fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
}

Then from Java you can call it like the following:

ContentKt.getContents((filePath, name, expiry, metadata) -> {
  return Unit.INSTANCE;
});

Use case #2 - function inside a kotlin file, changing the jvm name

You can also specify @file:JvmName(...) if you want to remove the kt that is added to the file name or if you want to give it any other logical name:

@file:JvmName("Content")

package test

fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
}

Then you can call it like that:

Content.getContents((filePath, name, expiry, metadata) -> {
    return Unit.INSTANCE;
});

Use case #3 - static function inside companion object or in an object

Given the following kotlin class:

class Content {
  companion object {
    @JvmStatic
    fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
    }
  }
}

Or the following object:

object Content {
  @JvmStatic
  fun getContents(callBack: (filePath: String, name: String, expiry: Int, metadata: String) -> Unit) {
  }
}

You will access it like that via Java:

Content.getContents((filePath, name, expiry, metadata) -> {
    return Unit.INSTANCE;
});

huangapple
  • 本文由 发表于 2020年8月12日 00:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定