{"id":146,"date":"2025-11-11T10:05:50","date_gmt":"2025-11-11T02:05:50","guid":{"rendered":"https:\/\/www.thebugmuxi.xyz\/?p=146"},"modified":"2025-11-11T10:05:52","modified_gmt":"2025-11-11T02:05:52","slug":"kotlin-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.thebugmuxi.xyz\/index.php\/2025\/11\/11\/kotlin-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95\/","title":{"rendered":"Kotlin \u57fa\u7840\u8bed\u6cd5"},"content":{"rendered":"\n<p>Kotlin \u6587\u4ef6\u4ee5&nbsp;<strong>.kt<\/strong>&nbsp;\u4e3a\u540e\u7f00\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5305\u58f0\u660e<\/h2>\n\n\n\n<p>\u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u4e00\u822c\u4e3a\u5305\u7684\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.runoob.main\n\nimport java.util.*\n\nfun test() {}\nclass Runoob {}<\/pre>\n\n\n\n<p>kotlin\u6e90\u6587\u4ef6\u4e0d\u9700\u8981\u76f8\u5339\u914d\u7684\u76ee\u5f55\u548c\u5305\uff0c\u6e90\u6587\u4ef6\u53ef\u4ee5\u653e\u5728\u4efb\u4f55\u6587\u4ef6\u76ee\u5f55\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4f8b\u4e2d test() \u7684\u5168\u540d\u662f com.runoob.main.test\u3001Runoob \u7684\u5168\u540d\u662f com.runoob.main.Runoob\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5305\uff0c\u9ed8\u8ba4\u4e3a&nbsp;<strong>default<\/strong>&nbsp;\u5305\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9ed8\u8ba4\u5bfc\u5165<\/h3>\n\n\n\n<p>\u6709\u591a\u4e2a\u5305\u4f1a\u9ed8\u8ba4\u5bfc\u5165\u5230\u6bcf\u4e2a Kotlin \u6587\u4ef6\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>kotlin.*\n\nkotlin.annotation.*\n\nkotlin.collections.*\n\nkotlin.comparisons.*\n\nkotlin.io.*\n\nkotlin.ranges.*\n\nkotlin.sequences.*\n\nkotlin.text.*<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6570\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u51fd\u6570\u5b9a\u4e49\u4f7f\u7528\u5173\u952e\u5b57 fun\uff0c\u53c2\u6570\u683c\u5f0f\u4e3a\uff1a\u53c2\u6570 : \u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun sum(a: Int, b: Int): Int {   \/\/ Int \u53c2\u6570\uff0c\u8fd4\u56de\u503c Int\n\u00a0\u00a0\u00a0\u00a0return a + b\n}<\/code><\/pre>\n\n\n\n<p>\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u51fd\u6570\u4f53\uff0c\u8fd4\u56de\u7c7b\u578b\u81ea\u52a8\u63a8\u65ad\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun sum(a: Int, b: Int) = a + b\n\npublic fun sum(a: Int, b: Int): Int = a + b   \/\/ public \u65b9\u6cd5\u5219\u5fc5\u987b\u660e\u786e\u5199\u51fa\u8fd4\u56de\u7c7b\u578b<\/code><\/pre>\n\n\n\n<p>\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570(\u7c7b\u4f3cJava\u4e2d\u7684void)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun printSum(a: Int, b: Int): Unit { \n\u00a0\u00a0\u00a0\u00a0print(a + b)\n}\n\n\n\/\/ \u5982\u679c\u662f\u8fd4\u56de Unit\u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u7701\u7565(\u5bf9\u4e8epublic\u65b9\u6cd5\u4e5f\u662f\u8fd9\u6837)\uff1a\npublic fun printSum(a: Int, b: Int) { \n\u00a0\u00a0\u00a0\u00a0print(a + b)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ef\u53d8\u957f\u53c2\u6570\u51fd\u6570<\/h3>\n\n\n\n<p>\u51fd\u6570\u7684\u53d8\u957f\u53c2\u6570\u53ef\u4ee5\u7528&nbsp;<strong>vararg<\/strong>&nbsp;\u5173\u952e\u5b57\u8fdb\u884c\u6807\u8bc6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun vars(vararg v:Int){\n    for(vt in v){\n        print(vt)\n    }\n}\n\n\/\/ \u6d4b\u8bd5\nfun main(args: Array&lt;String>) {\n    vars(1,2,3,4,5)  \/\/ \u8f93\u51fa12345\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">lambda(\u533f\u540d\u51fd\u6570)<\/h3>\n\n\n\n<p>lambda\u8868\u8fbe\u5f0f\u4f7f\u7528\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6d4b\u8bd5\nfun main(args: Array&lt;String>) {\n    val sumLambda: (Int, Int) -> Int = {x,y -> x+y}\n    println(sumLambda(1,2))  \/\/ \u8f93\u51fa 3\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u5e38\u91cf\u4e0e\u53d8\u91cf<\/h2>\n\n\n\n<p>\u53ef\u53d8\u53d8\u91cf\u5b9a\u4e49\uff1avar \u5173\u952e\u5b57<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var &lt;\u6807\u8bc6\u7b26&gt; : &lt;\u7c7b\u578b&gt; = &lt;\u521d\u59cb\u5316\u503c&gt;<\/pre>\n\n\n\n<p>\u4e0d\u53ef\u53d8\u53d8\u91cf\u5b9a\u4e49\uff1aval \u5173\u952e\u5b57\uff0c\u53ea\u80fd\u8d4b\u503c\u4e00\u6b21\u7684\u53d8\u91cf(\u7c7b\u4f3cJava\u4e2dfinal\u4fee\u9970\u7684\u53d8\u91cf)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">val &lt;\u6807\u8bc6\u7b26&gt; : &lt;\u7c7b\u578b&gt; = &lt;\u521d\u59cb\u5316\u503c&gt;<\/pre>\n\n\n\n<p>\u5e38\u91cf\u4e0e\u53d8\u91cf\u90fd\u53ef\u4ee5\u6ca1\u6709\u521d\u59cb\u5316\u503c,\u4f46\u662f\u5728\u5f15\u7528\u524d\u5fc5\u987b\u521d\u59cb\u5316<\/p>\n\n\n\n<p>\u7f16\u8bd1\u5668\u652f\u6301\u81ea\u52a8\u7c7b\u578b\u5224\u65ad,\u5373\u58f0\u660e\u65f6\u53ef\u4ee5\u4e0d\u6307\u5b9a\u7c7b\u578b,\u7531\u7f16\u8bd1\u5668\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>val a: Int = 1\nval b = 1       \/\/ \u7cfb\u7edf\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7c7b\u578b\u4e3aInt\nval c: Int      \/\/ \u5982\u679c\u4e0d\u5728\u58f0\u660e\u65f6\u521d\u59cb\u5316\u5219\u5fc5\u987b\u63d0\u4f9b\u53d8\u91cf\u7c7b\u578b\nc = 1           \/\/ \u660e\u786e\u8d4b\u503c\n\n\nvar x = 5        \/\/ \u7cfb\u7edf\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7c7b\u578b\u4e3aInt\nx += 1           \/\/ \u53d8\u91cf\u53ef\u4fee\u6539<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6ce8\u91ca<\/h2>\n\n\n\n<p>Kotlin \u652f\u6301\u5355\u884c\u548c\u591a\u884c\u6ce8\u91ca\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8fd9\u662f\u4e00\u4e2a\u5355\u884c\u6ce8\u91ca\n\n\/* \u8fd9\u662f\u4e00\u4e2a\u591a\u884c\u7684\n   \u5757\u6ce8\u91ca\u3002 *\/<\/code><\/pre>\n\n\n\n<p>\u4e0e Java \u4e0d\u540c, Kotlin \u4e2d\u7684\u5757\u6ce8\u91ca\u5141\u8bb8\u5d4c\u5957\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u7b26\u4e32\u6a21\u677f<\/h2>\n\n\n\n<p>$ \u8868\u793a\u4e00\u4e2a\u53d8\u91cf\u540d\u6216\u8005\u53d8\u91cf\u503c<\/p>\n\n\n\n<p>$varName \u8868\u793a\u53d8\u91cf\u503c<\/p>\n\n\n\n<p>${varName.fun()} \u8868\u793a\u53d8\u91cf\u7684\u65b9\u6cd5\u8fd4\u56de\u503c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>var a = 1\n\/\/ \u6a21\u677f\u4e2d\u7684\u7b80\u5355\u540d\u79f0\uff1a\nval s1 = \"a is $a\" \n\na = 2\n\/\/ \u6a21\u677f\u4e2d\u7684\u4efb\u610f\u8868\u8fbe\u5f0f\uff1a\nval s2 = \"${s1.replace(\"is\", \"was\")}, but now is $a\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">NULL\u68c0\u67e5\u673a\u5236<\/h2>\n\n\n\n<p>Kotlin\u7684\u7a7a\u5b89\u5168\u8bbe\u8ba1\u5bf9\u4e8e\u58f0\u660e\u53ef\u4e3a\u7a7a\u7684\u53c2\u6570\uff0c\u5728\u4f7f\u7528\u65f6\u8981\u8fdb\u884c\u7a7a\u5224\u65ad\u5904\u7406\uff0c\u6709\u4e24\u79cd\u5904\u7406\u65b9\u5f0f\uff0c\u5b57\u6bb5\u540e\u52a0!!\u50cfJava\u4e00\u6837\u629b\u51fa\u7a7a\u5f02\u5e38\uff0c\u53e6\u4e00\u79cd\u5b57\u6bb5\u540e\u52a0?\u53ef\u4e0d\u505a\u5904\u7406\u8fd4\u56de\u503c\u4e3a&nbsp;<strong>null<\/strong>&nbsp;\u6216\u914d\u5408&nbsp;<strong>?:<\/strong>&nbsp;\u505a\u7a7a\u5224\u65ad\u5904\u7406<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7c7b\u578b\u540e\u9762\u52a0?\u8868\u793a\u53ef\u4e3a\u7a7a\nvar age: String? = \"23\" \n\/\/\u629b\u51fa\u7a7a\u6307\u9488\u5f02\u5e38\nval ages = age!!.toInt()\n\/\/\u4e0d\u505a\u5904\u7406\u8fd4\u56de null\nval ages1 = age?.toInt()\n\/\/age\u4e3a\u7a7a\u8fd4\u56de-1\nval ages2 = age?.toInt() ?: -1<\/code><\/pre>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5f15\u7528\u53ef\u80fd\u4e3a null \u503c\u65f6, \u5bf9\u5e94\u7684\u7c7b\u578b\u58f0\u660e\u5fc5\u987b\u660e\u786e\u5730\u6807\u8bb0\u4e3a\u53ef\u4e3a null\u3002<\/p>\n\n\n\n<p>\u5f53 str \u4e2d\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u4e0d\u662f\u4e00\u4e2a\u6574\u6570\u65f6, \u8fd4\u56de null:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun parseInt(str: String): Int? {\n  \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u4e00\u4e2a\u8fd4\u56de\u503c\u53ef\u4e3a null \u7684\u51fd\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun parseInt(str: String): Int? {\n    return str.toIntOrNull()\n}\n\nfun printProduct(arg1: String, arg2: String) {\n    val x = parseInt(arg1)\n    val y = parseInt(arg2)\n\n    \/\/ \u76f4\u63a5\u4f7f\u7528 `x * y` \u4f1a\u5bfc\u81f4\u9519\u8bef, \u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u4e3a null\n    if (x != null &amp;&amp; y != null) {\n        \/\/ \u5728\u8fdb\u884c\u8fc7 null \u503c\u68c0\u67e5\u4e4b\u540e, x \u548c y \u7684\u7c7b\u578b\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a\u975e null \u53d8\u91cf\n        println(x * y)\n    }\n    else {\n        println(\"'$arg1' or '$arg2' is not a number\")\n    }    \n}\n\nfun main() {\n    printProduct(\"6\", \"7\")\n    printProduct(\"a\", \"7\")\n    printProduct(\"a\", \"b\")\n}<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun parseInt(str: String): Int? {\n    return str.toIntOrNull()\n}\n\nfun printProduct(arg1: String, arg2: String) {\n    val x = parseInt(arg1)\n    val y = parseInt(arg2)\n    \n    \/\/ ...\n    if (x == null) {\n        println(\"Wrong number format in arg1: '$arg1'\")\n        return\n    }\n    if (y == null) {\n        println(\"Wrong number format in arg2: '$arg2'\")\n        return\n    }\n\n    \/\/ \u5728\u8fdb\u884c\u8fc7 null \u503c\u68c0\u67e5\u4e4b\u540e, x \u548c y \u7684\u7c7b\u578b\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a\u975e null \u53d8\u91cf\n    println(x * y)\n}\n\nfun main() {\n    printProduct(\"6\", \"7\")\n    printProduct(\"a\", \"7\")\n    printProduct(\"99\", \"b\")\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u578b\u68c0\u6d4b\u53ca\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362<\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528&nbsp;<strong>is<\/strong>&nbsp;\u8fd0\u7b97\u7b26\u68c0\u6d4b\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u6307\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b(\u7c7b\u4f3c\u4e8e Java \u4e2d\u7684 instanceof \u5173\u952e\u5b57)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>if (obj is Type) {\n    \/\/ \u5982\u679c obj \u662f Type \u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 Type \u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\n} else {\n    \/\/ \u5904\u7406\u5176\u4ed6\u7c7b\u578b\u60c5\u51b5\n}<\/code><\/pre>\n\n\n\n<p>\u5f53&nbsp;<code>is<\/code>&nbsp;\u68c0\u6d4b\u901a\u8fc7\u65f6\uff0cKotlin \u4f1a\u81ea\u52a8\u5c06&nbsp;<code>obj<\/code>&nbsp;\u89c6\u4e3a\u6307\u5b9a\u7c7b\u578b\uff0c\u56e0\u6b64\u5728&nbsp;<code>if<\/code>&nbsp;\u8bed\u53e5\u7684\u5206\u652f\u5185\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u8fd9\u88ab\u79f0\u4e3a<strong>\u667a\u80fd\u7c7b\u578b\u8f6c\u6362<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() {\n\u00a0 \u00a0 val obj: Any = \"Hello, Kotlin\"\n\n\u00a0 \u00a0 if (obj is String) {\n\u00a0 \u00a0 \u00a0 \u00a0 println(\"\u5b57\u7b26\u4e32\u957f\u5ea6: ${obj.length}\") \/\/ \u5728\u8fd9\u91cc `obj` \u5df2\u88ab\u667a\u80fd\u8f6c\u6362\u4e3a `String`\n\u00a0 \u00a0 } else {\n\u00a0 \u00a0 \u00a0 \u00a0 println(\"\u4e0d\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\")\n\u00a0 \u00a0 }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c&nbsp;<code>obj<\/code>&nbsp;\u662f&nbsp;<code>String<\/code>&nbsp;\u7c7b\u578b\uff0c<code>is<\/code>&nbsp;\u8fd0\u7b97\u7b26\u8fd4\u56de&nbsp;<code>true<\/code>\uff0cKotlin \u4f1a\u5c06&nbsp;<code>obj<\/code>&nbsp;\u8bc6\u522b\u4e3a&nbsp;<code>String<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee&nbsp;<code>String<\/code>&nbsp;\u7c7b\u578b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u5e94\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getStringLength(obj: Any): Int? {\n  if (obj is String) {\n    \/\/ \u505a\u8fc7\u7c7b\u578b\u5224\u65ad\u4ee5\u540e\uff0cobj\u4f1a\u88ab\u7cfb\u7edf\u81ea\u52a8\u8f6c\u6362\u4e3aString\u7c7b\u578b\n    return obj.length \n  }\n\n  \/\/\u5728\u8fd9\u91cc\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e0eJava\u4e2dinstanceof\u4e0d\u540c\uff0c\u4f7f\u7528!is\n  \/\/ if (obj !is String){\n  \/\/   \/\/ XXX\n  \/\/ }\n\n  \/\/ \u8fd9\u91cc\u7684obj\u4ecd\u7136\u662fAny\u7c7b\u578b\u7684\u5f15\u7528\n  return null\n}<\/code><\/pre>\n\n\n\n<p>\u6216\u8005<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getStringLength(obj: Any): Int? {\n  if (obj !is String)\n    return null\n  \/\/ \u5728\u8fd9\u4e2a\u5206\u652f\u4e2d, `obj` \u7684\u7c7b\u578b\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a `String`\n  return obj.length\n}<\/code><\/pre>\n\n\n\n<p>\u751a\u81f3\u8fd8\u53ef\u4ee5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getStringLength(obj: Any): Int? {\n  \/\/ \u5728 `&amp;&amp;` \u8fd0\u7b97\u7b26\u7684\u53f3\u4fa7, `obj` \u7684\u7c7b\u578b\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a `String`\n  if (obj is String &amp;&amp; obj.length > 0)\n    return obj.length\n  return null\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u533a\u95f4<\/h2>\n\n\n\n<p>\u533a\u95f4\u8868\u8fbe\u5f0f\u7531\u5177\u6709\u64cd\u4f5c\u7b26\u5f62\u5f0f&nbsp;<strong>..<\/strong>&nbsp;\u7684 rangeTo \u51fd\u6570\u8f85\u4ee5 in \u548c !in \u5f62\u6210\u3002<\/p>\n\n\n\n<p>\u533a\u95f4\u662f\u4e3a\u4efb\u4f55\u53ef\u6bd4\u8f83\u7c7b\u578b\u5b9a\u4e49\u7684\uff0c\u4f46\u5bf9\u4e8e\u6574\u578b\u539f\u751f\u7c7b\u578b\uff0c\u5b83\u6709\u4e00\u4e2a\u4f18\u5316\u7684\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u533a\u95f4\u7684\u4e00\u4e9b\u793a\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>for (i in 1..4) print(i) \/\/ \u8f93\u51fa\u201c1234\u201d\n\nfor (i in 4..1) print(i) \/\/ \u4ec0\u4e48\u90fd\u4e0d\u8f93\u51fa\n\nif (i in 1..10) { \/\/ \u7b49\u540c\u4e8e 1 &lt;= i &amp;&amp; i &lt;= 10\n    println(i)\n}\n\n\/\/ \u4f7f\u7528 step \u6307\u5b9a\u6b65\u957f\nfor (i in 1..4 step 2) print(i) \/\/ \u8f93\u51fa\u201c13\u201d\n\nfor (i in 4 downTo 1 step 2) print(i) \/\/ \u8f93\u51fa\u201c42\u201d\n\n\n\/\/ \u4f7f\u7528 until \u51fd\u6570\u6392\u9664\u7ed3\u675f\u5143\u7d20\nfor (i in 1 until 10) {   \/\/ i in &#91;1, 10) \u6392\u9664\u4e86 10\n     println(i)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u4f8b\u6d4b\u8bd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main(args: Array&lt;String>) {\n    print(\"\u5faa\u73af\u8f93\u51fa\uff1a\")\n    for (i in 1..4) print(i) \/\/ \u8f93\u51fa\u201c1234\u201d\n    println(\"\\n----------------\")\n    print(\"\u8bbe\u7f6e\u6b65\u957f\uff1a\")\n    for (i in 1..4 step 2) print(i) \/\/ \u8f93\u51fa\u201c13\u201d\n    println(\"\\n----------------\")\n    print(\"\u4f7f\u7528 downTo\uff1a\")\n    for (i in 4 downTo 1 step 2) print(i) \/\/ \u8f93\u51fa\u201c42\u201d\n    println(\"\\n----------------\")\n    print(\"\u4f7f\u7528 until\uff1a\")\n    \/\/ \u4f7f\u7528 until \u51fd\u6570\u6392\u9664\u7ed3\u675f\u5143\u7d20\n    for (i in 1 until 4) {   \/\/ i in &#91;1, 4) \u6392\u9664\u4e86 4\n        print(i)\n    }\n    println(\"\\n----------------\")\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5faa\u73af\u8f93\u51fa\uff1a1234\n----------------\n\u8bbe\u7f6e\u6b65\u957f\uff1a13\n----------------\n\u4f7f\u7528 downTo\uff1a42\n----------------\n\u4f7f\u7528 until\uff1a123\n----------------<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin \u6587\u4ef6\u4ee5&nbsp;.kt&nbsp;\u4e3a\u540e\u7f00\u3002 \u5305\u58f0\u660e \u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u4e00\u822c\u4e3a\u5305\u7684\u58f0\u660e\uff1a packa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,14],"tags":[33,8,34],"class_list":["post-146","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin","category-14","tag-kotlin","tag-8","tag-34"],"_links":{"self":[{"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":2,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions\/154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thebugmuxi.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}