Kotlin 科特林任意-双误匹配
我试图传递可以存储Double、Int、Long等值的数组Kotlin 科特林任意-双误匹配,kotlin,Kotlin,我试图传递可以存储Double、Int、Long等值的数组 val input = arrayOf(1.3, 4.5) val output = arrayOf(3) // Error Kotlin: Type mismatch: inferred type is Array<Int> but Array<Any> was expected magic(input, output) fun magic(input: Array<Any>, output:
val input = arrayOf(1.3, 4.5)
val output = arrayOf(3) // Error Kotlin: Type mismatch: inferred type is Array<Int> but Array<Any> was expected
magic(input, output)
fun magic(input: Array<Any>, output: Array<Any>) {
// Do the magic
}
val输入=arrayOf(1.3,4.5)
val output=arrayOf(3)//错误Kotlin:类型不匹配:推断的类型为数组,但应为数组
魔术(输入、输出)
趣味魔术(输入:数组,输出:数组){
//施展魔法
}
我必须使用哪种类型的参数才能做到这一点?您可能正在寻找
fun magic(输入:数组,输出:数组){
//施展魔法
}
val输入=arrayOf(1.3,4.5)
val输出=阵列(3)
魔术(输入、输出)
根据您的需求,您可以使用编号
类。Kotlin网站上的文档说明-Number
是“代表数值的所有平台类的超类”
您可以按如下方式修改magic
函数-
fun magic(input: Array<Number>, output: Array<Number>) {
// Do the magic
}
fun magic(输入:数组,输出:数组){
//施展魔法
}
然后,通过构造函数所需的参数来调用函数-
val input = arrayOf<Number>(1.3, 4.5)
val output = arrayOf<Number>(3)
magic(input, output)
val输入=arrayOf(1.3,4.5)
val输出=阵列(3)
魔术(输入、输出)
看起来你的版本比我的版本晚了一点,几乎一样,如果你认为可以添加一些内容,请随意编辑我的版本
val input = arrayOf<Number>(1.3, 4.5)
val output = arrayOf<Number>(3)
magic(input, output)