{"id":1210,"date":"2022-05-28T22:37:13","date_gmt":"2022-05-28T14:37:13","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1210"},"modified":"2024-03-21T17:04:29","modified_gmt":"2024-03-21T09:04:29","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/05\/28\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%881%ef%bc%89\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60\uff081\uff09"},"content":{"rendered":"\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Kotlin\u53d8\u91cf\u4e0e\u51fd\u6570<\/p>\n\n\n\n<!--more-->\n\n\n\n<p style=\"font-size:25px\">\u53d8\u91cf\uff1a<\/p>\n\n\n\n<p>Kotlin\u4e2d\u7684\u53d8\u91cf\u5b9a\u4e49\u548cJava\u4e2d\u5dee\u522b\u5f88\u5927\uff0c\u4e0eJava\u4e2d\u9700\u8981\u7ed9\u53d8\u91cf\u660e\u786e\u5b9a\u4e49\u53d8\u91cf\u7c7b\u578b\u4e0d\u540c\uff0cKotlin\u53ea\u5141\u8bb8\u53d8\u91cf\u524d\u58f0\u660e\u4e24\u79cd\u5173\u952e\u5b57\uff1a<code>val<\/code>\u548c<code>var<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>val: \uff08value\uff09\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u53d8\u91cf\uff0c\u8fd9\u79cd\u53d8\u91cf\u5728\u521d\u59cb\u8d4b\u503c\u4e4b\u540e\u5c31\u518d\u4e5f\u4e0d\u80fd\u91cd\u65b0\u8d4b\u503c\uff0c\u5bf9\u5e94Java\u4e2d\u7684final\u53d8\u91cf\u3002<\/li><li>var:\uff08variable\u7684\u7b80\u5199\uff09\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u53ef\u53d8\u7684\u53d8\u91cf\uff0c\u8fd9\u79cd\u53d8\u91cf\u5728\u521d\u59cb\u8d4b\u503c\u4e4b\u540e\u4ecd\u7136\u53ef\u4ee5\u518d\u88ab\u91cd\u65b0\u8d4b\u503c\uff0c\u5bf9\u5e94Java\u4e2d\u7684\u975efinal\u53d8\u91cf\u3002<\/li><\/ol>\n\n\n\n<p>Kotlin\u6709\u7740\u4f18\u79c0\u7684\u7c7b\u578b\u63a8\u5bfc\u673a\u5236\uff0c\u53ef\u4ee5\u5b8c\u6210\u7c7b\u578b\u7684\u81ea\u52a8\u63a8\u5bfc\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u8fd9\u4e24\u79cd\u5173\u952e\u5b57\u4fbf\u53ef\u3002<\/p>\n\n\n\n<p>\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun main() {\n  val a = 10\n  println(\"a = \" + a)\n}<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u7684\u662f\uff0c\u4e0eJava\u4e0d\u540cKotlin\u6bcf\u4e00\u884c\u7ed3\u5c3e\u4e0d\u7528\u52a0\u5206\u53f7\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\uff0c\u9664\u4e86\u8ba9\u7a0b\u5e8f\u81ea\u52a8\u63a8\u5bfc\u53d8\u91cf\u7c7b\u578b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u663e\u5f0f\u5730\u8bbe\u5b9a\u53d8\u91cf\u7c7b\u578b\uff0c\u4f7f\u7528\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val a: Int = 10<\/code><\/pre>\n\n\n\n<p>\u5c06a\u53d8\u91cf\u58f0\u660e\u4e3a\u4e86Int\u7c7b\u578b<\/p>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\">\u51fd\u6570\uff1a<\/p>\n\n\n\n<p>Kotlin\u5141\u8bb8\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u51fd\u6570\uff0c\u57fa\u672c\u8bed\u6cd5\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun methodName(param1: Int, param2: Int): Int {\n   return 0\n}<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\uff0c\u51fd\u6570\u7684\u8bed\u6cd5\u9700\u8981\u4f7f\u7528 <code>fun{}<\/code>\u6765\u5305\u88f9\uff0c\u7d27\u8ddf\u7740\u7684\u5c31\u662f\u51fd\u6570\u540d<\/p>\n\n\n\n<p>\u4e4b\u540e\u62ec\u53f7\u5185\u8868\u793a\u58f0\u660e\u8be5\u51fd\u6570\u63a5\u53d7\u4ec0\u4e48\u53c2\u6570\uff0c\u91c7\u7528 \u201c\u53c2\u6570\u540d: \u53c2\u6570\u7c7b\u578b\u201d \u65b9\u5f0f\u6765\u5b9a\u4e49\u3002<\/p>\n\n\n\n<p>\u62ec\u53f7\u4e4b\u540e\u4f7f\u7528 &#8220;: \u6570\u636e\u7c7b\u578b&#8221; \u8868\u793a\u8be5\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b\uff0c\u5982\u679c\u6ca1\u6709\u5219\u8868\u793a\u4e0d\u8fd4\u56de\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-medium-font-size\">\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u91cc\u6709\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff1a<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u51fd\u6570\u4e2d\u53ea\u6709\u4e00\u884c\u4ee3\u7801\u65f6\uff0cKotlin\u5141\u8bb8\u6211\u4eec\u4e0d\u5fc5\u7f16\u5199\u51fd\u6570\u4f53\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u552f\u4e00\u7684\u4e00\u884c\u4ee3\u7801\u5199\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u5c3e\u90e8\uff0c\u4e2d\u95f4\u7528\u7b49\u53f7\u8fde\u63a5\u5373\u53ef\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun largerNumber(num1: Int, num2: Int): Int = max(num1, num2)<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u8fd9\u79cd\u8bed\u6cd5\uff0creturn\u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7701\u7565\u4e86\uff0c\u7b49\u53f7\u8db3\u4ee5\u8868\u8fbe\u8fd4\u56de\u503c\u7684\u610f\u601d\u3002\u4e14\u7531\u4e8eKotlin\u7684\u63a8\u5bfc\u673a\u5236\uff0c\u8fd9\u91cc\u53ef\u4ee5\u7b80\u5316\u6389\u8fd4\u56deInt\u7c7b\u578b\u7684\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun largerNumber(num1: Int, num2: Int) = max(num1, num2)<\/code><\/pre>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">if\u6761\u4ef6\u8bed\u53e5<\/p>\n\n\n\n<p>Kotlin\u4e2d\u7684if\u4f7f\u7528\u65b9\u6cd5\u4e0eJava\u4e2d\u51e0\u4e4e\u76f8\u540c\uff0c\u4f46\u5728\u4e00\u4e9b\u5730\u65b9\u8fdb\u884c\u4e86\u4f18\u5316\uff1a<\/p>\n\n\n\n<p>Kotlin\u4e2d\u7684if\u8bed\u53e5\u76f8\u6bd4\u4e8eJava\u6709\u4e00\u4e2a\u989d\u5916\u7684\u529f\u80fd\uff0c\u5b83\u662f\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u7684\uff0c\u8fd4\u56de\u503c\u5c31\u662fif\u8bed\u53e5\u6bcf\u4e00\u4e2a\u6761\u4ef6\u4e2d\u6700\u540e\u4e00\u884c\u4ee3\u7801\u7684\u8fd4\u56de\u503c\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun largerNumber(num1: Int, num2: Int): Int {\n  val value = if (num1 > num2) {\n    num1\n  } else {\n    num2\n  }\n  return value\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u53d8\u91cf\u4e4b\u95f4\u7b49\u4e8eif\u8bed\u53e5\uff0c\u800c\u7ed3\u679c\u4e3a\u5bf9\u5e94if\u7684\u6700\u540e\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u7684\u7528\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7528\u5728\u8d4b\u503c\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5728\u51fd\u6570\u8fd4\u56de\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun largerNumber(num1: Int, num2: Int): Int {\n    return if (num1 > num2) {\n      num1\n    } else {\n      num2\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u751a\u81f3\u76f4\u63a5\u7528\u5728\u51fd\u6570\u5355\u884c\u8bed\u53e5\u8bed\u6cd5\u7cd6\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun largerNumber(num1: Int, num2: Int) = if (num1 > num2) {\n    num1\n  } else {\n    num2\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\u8fd9\u91cc\u7684{}\u4e5f\u662f\u53ef\u4ee5\u53bb\u6389\u5f62\u6210\u5355\u884c\u8bed\u53e5\u7684\uff0c\u540c\u5176\u4ed6\u8bed\u8a00\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">when\u6761\u4ef6\u8bed\u53e5<\/p>\n\n\n\n<p>Kotlin\u4e2d\u7684when\u6761\u4ef6\u8bed\u53e5\u7c7b\u4f3c\u4e8eJava\u4e2d\u7684switch\uff0c\u4f46\u53c8\u6bd4switch\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0cswitch\u53ea\u80fd\u4f20\u5165\u6574\u578b\u6216\u77ed\u4e8e\u6574\u578b\u7684\u53d8\u91cf\u4f5c\u4e3a\u6761\u4ef6\uff0cJDK 1.7\u4e4b\u540e\u589e\u52a0\u4e86\u5bf9\u5b57\u7b26\u4e32\u53d8\u91cf\u7684\u652f\u6301\uff0c\u4f46\u5982\u679c\u4f60\u7684\u5224\u65ad\u903b\u8f91\u4f7f\u7528\u7684\u5e76\u975e\u662f\u4e0a\u8ff0\u51e0\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u4e0d\u597d\u610f\u601d\uff0cswitch\u5e76\u4e0d\u9002\u5408\u4f60\u3002\u5176\u6b21\uff0cswitch\u4e2d\u7684\u6bcf\u4e2acase\u6761\u4ef6\u90fd\u8981\u5728\u6700\u540e\u4e3b\u52a8\u52a0\u4e0a\u4e00\u4e2abreak\uff0c\u5426\u5219\u6267\u884c\u5b8c\u5f53\u524dcase\u4e4b\u540e\u4f1a\u4f9d\u6b21\u6267\u884c\u4e0b\u9762\u7684case\uff0c\u8fd9\u4e00\u7279\u6027\u66fe\u7ecf\u5bfc\u81f4\u8fc7\u65e0\u6570\u5947\u602a\u7684bug\uff0c\u5c31\u662f\u56e0\u4e3a\u6709\u4eba\u5fd8\u8bb0\u6dfb\u52a0break\u3002<\/p>\n\n\n\n<p>when\u6761\u4ef6\u8bed\u53e5\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun getScore(name: String) = when (name) {\n     \"Tom\" -> 86\n     \"Jim\" -> 77\n     \"Jack\" -> 95\n     \"Lily\" -> 100\n     else -> 0\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5728when\u540e\u9762\u62ec\u53f7\u5185\u5b9a\u4e49\u8981\u6761\u4ef6\u6e90\uff0c\u4f7f\u7528 \u201c\u5339\u914d\u503c -> { \u6267\u884c\u903b\u8f91 }\u201d \u7684\u8bed\u53e5\u6765\u5b8c\u6210<\/p>\n\n\n\n<p>\u4f7f\u7528else -> \u8868\u793a\u9ed8\u8ba4\u503c<\/p>\n\n\n\n<p>\u5bf9\u6bd4if\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\"><code>fun getScore(name: String) = if (name == \"Tom\") {<\/code>   86\n} else if (name == \"Jim\") {\n   77\n} else if (name == \"Jack\") {\n   95\n} else if (name == \"Lily\") {\n   100\n} else {\n   0\n}<\/code><\/pre>\n\n\n\n<p>when\u8bed\u53e5\u5141\u8bb8\u4f20\u5165\u4e00\u4e2a\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u7136\u540e\u53ef\u4ee5\u5728when\u7684\u7ed3\u6784\u4f53\u4e2d\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u7cbe\u786e\u5339\u914d\u4e4b\u5916\uff0cwhen\u8bed\u53e5\u8fd8\u5141\u8bb8\u8fdb\u884c\u7c7b\u578b\u5339\u914d\u3002\u4ec0\u4e48\u662f\u7c7b\u578b\u5339\u914d\u5462\uff1f\u8fd9\u91cc\u6211\u518d\u4e3e\u4e2a\u4f8b\u5b50\u3002\u5b9a\u4e49\u4e00\u4e2acheckNumber()\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun checkNumber(num: Number) {\n     when (num) {\n        is Int -> println(\"number is Int\")\n        is Double -> println(\"number is Double\")\n        else -> println(\"number not support\")\n     }\n}<\/code><\/pre>\n\n\n\n<p>is\u5173\u952e\u5b57\u5c31\u662f\u7c7b\u578b\u5339\u914d\u7684\u6838\u5fc3\uff0c\u5b83\u76f8\u5f53\u4e8eJava\u4e2d\u7684instanceof\u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p>\u7531\u4e8echeckNumber()\u51fd\u6570\u63a5\u6536\u4e00\u4e2aNumber\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd9\u662fKotlin\u5185\u7f6e\u7684\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u50cfInt\u3001Long\u3001Float\u3001Double\u7b49\u4e0e\u6570\u5b57\u76f8\u5173\u7684\u7c7b\u90fd\u662f\u5b83\u7684\u5b50\u7c7b\uff0c\u6240\u4ee5\u8fd9\u91cc\u5c31\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u5339\u914d\u6765\u5224\u65ad\u4f20\u5165\u7684\u53c2\u6570\u5230\u5e95\u5c5e\u4e8e\u4ec0\u4e48\u7c7b\u578b\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u5faa\u73af\u8bed\u53e5<\/p>\n\n\n\n<p>\u4e0eJava\u76f8\u540c\uff0cKotlin\u4e5f\u63d0\u4f9b\u4e86while\u548cfor\u4e24\u79cd\u5faa\u73af\u65b9\u6cd5\uff0cwhile\u4e0eJava\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u4f46Kotlin\u5728for\u5faa\u73af\u4e0a\u505a\u4e86\u5f88\u5927\u7a0b\u5ea6\u7684\u4fee\u6539\u3002Java\u4e2d\u6700\u5e38\u7528\u7684for-i\u5faa\u73af\u5728Kotlin\u4e2d\u76f4\u63a5\u88ab\u820d\u5f03\u4e86\uff0c\u800cJava\u4e2d\u53e6\u4e00\u79cdfor-each\u5faa\u73af\u5219\u88abKotlin\u8fdb\u884c\u4e86\u5927\u5e45\u5ea6\u7684\u52a0\u5f3a\uff0c\u53d8\u6210\u4e86for-in\u5faa\u73af\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u9700\u8981\u666e\u53ca\u4e00\u4e2a\u533a\u95f4\u7684\u6982\u5ff5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val range = 0..10<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u8868\u793a\u521b\u5efa\u4e86\u4e00\u4e2a0\u523010\u7684\u533a\u95f4\uff0c\u5e76\u4e14\u4e24\u7aef\u90fd\u662f\u95ed\u533a\u95f4\uff0c\u8fd9\u610f\u5473\u77400\u523010\u8fd9\u4e24\u4e2a\u7aef\u70b9\u90fd\u662f\u5305\u542b\u5728\u533a\u95f4\u4e2d\u7684\uff0c\u7528\u6570\u5b66\u7684\u65b9\u5f0f\u8868\u8fbe\u51fa\u6765\u5c31\u662f[0, 10]\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\uff0c..\u662f\u521b\u5efa\u4e24\u7aef\u95ed\u533a\u95f4\u7684\u5173\u952e\u5b57\uff0c\u5728..\u7684\u4e24\u8fb9\u6307\u5b9a\u533a\u95f4\u7684\u5de6\u53f3\u7aef\u70b9\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u533a\u95f4\u4e86\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\u53ef\u4ee5\u4f7f\u7528\u533a\u95f4\u6765\u5b8c\u6210for-in\u5faa\u73af\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun main() {\n    for (i in 0..10) {\n      println(i)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u53cc\u7aef\u95ed\u533a\u95f4\u5374\u4e0d\u5982\u5355\u7aef\u95ed\u533a\u95f4\u597d\u7528\u3002\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8bf4\u5462\uff1f\u76f8\u4fe1\u4f60\u4e00\u5b9a\u77e5\u9053\u6570\u7ec4\u7684\u4e0b\u6807\u90fd\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684\u6570\u7ec4\uff0c\u5b83\u7684\u4e0b\u6807\u533a\u95f4\u8303\u56f4\u662f0\u52309\uff0c\u56e0\u6b64\u5de6\u95ed\u53f3\u5f00\u7684\u533a\u95f4\u5728\u7a0b\u5e8f\u8bbe\u8ba1\u5f53\u4e2d\u66f4\u52a0\u5e38\u7528\u3002Kotlin\u4e2d\u53ef\u4ee5\u4f7f\u7528until\u5173\u952e\u5b57\u6765\u521b\u5efa\u4e00\u4e2a\u5de6\u95ed\u53f3\u5f00\u7684\u533a\u95f4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val range = 0 until 10<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u8868\u793a\u521b\u5efa\u4e86\u4e00\u4e2a0\u523010\u7684\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff0c\u5b83\u7684\u6570\u5b66\u8868\u8fbe\u65b9\u5f0f\u662f[0, 10)\u3002\u4fee\u6539main()\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\uff0c\u4f7f\u7528until\u66ff\u4ee3..\u5173\u952e\u5b57\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u6700\u540e\u4e00\u884c10\u4e0d\u4f1a\u518d\u6253\u5370\u51fa\u6765\u4e86\u3002<br><\/p>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cfor-in\u5faa\u73af\u6bcf\u6b21\u6267\u884c\u5faa\u73af\u65f6\u4f1a\u5728\u533a\u95f4\u8303\u56f4\u5185\u9012\u589e1\uff0c\u76f8\u5f53\u4e8eJava for-i\u5faa\u73af\u4e2d<br>i++\u7684\u6548\u679c\uff0c\u800c\u5982\u679c\u4f60\u60f3\u8df3\u8fc7\u5176\u4e2d\u7684\u4e00\u4e9b\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528step\u5173\u952e\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun main() {\n    for (i in 0 until 10 step 2) {\n       println(i)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u76f8\u5f53\u4e8efor-i\u5faa\u73af\u4e2di = i + 2\u7684\u6548\u679c<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u964d\u5e8f\u7684\u533a\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528downTo\u5173\u952e\u5b57<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun main() {\n   for (i in 10 downTo 1) {\n        println(i)\n   }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a[10, 1]\u7684\u964d\u5e8f\u533a\u95f4\u3002\u964d\u5e8f\u533a\u95f4\u4e5f\u53ef\u4ee5\u4f7f\u7528step\u8bed\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin\u53d8\u91cf\u4e0e\u51fd\u6570<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[1,16],"tags":[],"class_list":["post-1210","post","type-post","status-publish","format-standard","hentry","category-wcd","category-16"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1210"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1210"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1210\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1210\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}