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関数と見なすものの例です。それらは実行されているプラ​​ットフォームに基づいて定義されており、ソース コードでそれらの実装を見つけることはできません。

java.lang.StringJVM の場合、それらは対応するネイティブメソッドに直接マップされます。これにより、実行時のオーバーヘッドがなくなり、Java 標準ライブラリで行われる最適化が活用されます。

于 2017-06-13T14:15:41.510 に答える