6

在 kotlin 源代码中,我无法理解如何实现 String.kt 的长度,如下所示:

package kotlin                                                  
public class String : Comparable<String>, CharSequence {
companion object {}

/**
 * Returns a string obtained by concatenating this string with the string representation of the given [other] object.
 */
public operator fun plus(other: Any?): String

public override val length: Int

public override fun get(index: Int): Char

public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence

public override fun compareTo(other: String): Int}

var len:Int = "abc".length; // len = 3 where to run the length??

在哪里实现长度功能?

4

1 回答 1

13

字符串函数是 Kotlin 认为的Intrinsic函数的示例。它们是根据它们运行的​​平台定义的,您将无法在源代码中找到它们的实现。

对于 JVM,它们将直接映射到相应的本地java.lang.String方法。这确保了没有运行时开销,并利用了 java 标准库中的优化。

于 2017-06-13T14:15:41.510 回答