gpt4 book ai didi

kotlin - 从 kotlin 调用 java varargs 方法

转载 作者:IT老高 更新时间:2023-10-28 13:37:13 28 4
gpt4 key购买 nike

我有一个java函数:

public static void initialize(@NonNull Activity activity, Settings... settings) {}

我想从 kotlin 调用它:

fun initialize(activity: Activity, vararg settings: settings) = JavaClass.initialize(activity, settings)

但是编译不出来,告诉我有类型不匹配,Settings是必需的,但参数是 kotlin.Array<out Settings>

我看到它正在尝试将其与签名匹配

public static void initialize(@NonNull Activity activity, Settings settings) {}

但我想用

public static void initialize(@NonNull Activity activity, Settings[] settings) {}

最佳答案

您应该使用以下语法:

fun initialize(activity: Activity, vararg settings: settings) =
JavaClass.initialize(activity, *settings)

https://kotlinlang.org/docs/reference/java-interop.html#java-varargs

关于kotlin - 从 kotlin 调用 java varargs 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36626152/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com