我读过What is the Kotlin exponent operator,并试图按照它的答案编写val t1 = 23.0,然后在模式下编写print (t1!!.pow(4.9)),并获得第二个错误:未解决的引用: pow。在其他地方通过搜索Kotlin幂函数找到的代码运行良好的print (Math.pow(t1,4.0))。我感到很困惑,但我发现Unresolved reference: pow in Eclipse using Kotlin后,当我做import kotlin.math.pow next print (t1.pow(4.9))开始给出一个数字。我还注意到import kotlin.Math.pow给出了错误:未解析的引用:数学,所以
pow w/out在REPL中导入类似于t1.math.pow(2.3)的内容(因为它会给出错误:未解决的引用:数学发布于 2019-09-13 04:16:00
使用Math.pow(10.0, 2.0)时
println(Math.pow(10.0, 2.0)) // "100.0"Math指的是java.lang.Math类。
您可以通过调用
println(Math::class) // "java.lang.Math"在这种情况下,不需要导入任何东西,因为默认情况下java.lang包是导入的。
使用10.0.pow(2)时
println(10.0.pow(2)) // "100.0"pow是指fun Double.pow(x: Double): Double,这是需要从kotlin.math.pow显式导入的Kotlin扩展函数。
import kotlin.math.powhttps://stackoverflow.com/questions/57917095
复制相似问题