{"id":166,"date":"2025-11-19T14:37:57","date_gmt":"2025-11-19T06:37:57","guid":{"rendered":"https:\/\/www.tangent0712.top\/?p=166"},"modified":"2025-11-19T14:37:57","modified_gmt":"2025-11-19T06:37:57","slug":"kotlin-01-%e7%ae%80%e5%8d%95%e7%a8%8b%e5%ba%8f-%e5%8f%98%e9%87%8f-%e5%ae%b9%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.tangent0712.top\/index.php\/2025\/11\/19\/kotlin-01-%e7%ae%80%e5%8d%95%e7%a8%8b%e5%ba%8f-%e5%8f%98%e9%87%8f-%e5%ae%b9%e5%99%a8\/","title":{"rendered":"Kotlin 01 \u7b80\u5355\u7a0b\u5e8f \u53d8\u91cf \u5bb9\u5668"},"content":{"rendered":"<h2>1 \u7b80\u5355\u7684Kotlin\u7a0b\u5e8f<\/h2>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">fun main() {\n    println(&quot;HelloWorld!&quot;)\n    \/\/ System:HelloWorld!\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7b80\u5355\u7a0b\u5e8f\u523b\u753b\u4e86Kotlin\u7a0b\u5e8f\u7684\u57fa\u672c\u7ed3\u6784:<\/p>\n<ul>\n<li><code>fun<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<\/li>\n<li>\u540d\u4e3a <code>main<\/code> \u7684\u51fd\u6570\u662f\u6574\u4e2a\u7a0b\u5e8f\u7684\u5165\u53e3<\/li>\n<li><code>println<\/code> \u8bed\u53e5\u7684\u4f5c\u7528\u662f\u628a\u5b57\u7b26\u4e32\u6253\u5370\u5230\u63a7\u5236\u53f0<\/li>\n<li>\u8bed\u53e5\u540e\u4e0d\u9700\u8981\u52a0 <code>;<\/code><\/li>\n<\/ul>\n<h2>2 \u53d8\u91cf<\/h2>\n<h3>2.1 <code>val<\/code> \u5173\u952e\u5b57\u548c <code>var<\/code> \u5173\u952e\u5b57<\/h3>\n<ul>\n<li><code>val<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u53ea\u8bfb\u53d8\u91cf,\u5373\u53d8\u91cf\u6307\u5411\u7684\u4f4d\u7f6e\u4e0d\u53ef\u6539\u53d8<\/li>\n<li><code>var<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u53ef\u53d8\u53d8\u91cf,\u53d8\u91cf\u6307\u5411\u7684\u4f4d\u7f6e\u662f\u53ef\u53d8\u7684<\/li>\n<\/ul>\n<p>\u5728\u540e\u7eed\u4e0d\u9700\u8981\u66f4\u6539\u53d8\u91cf\u65f6,\u4f18\u5148\u4f7f\u7528 <code>val<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u53ea\u8bfb\u53d8\u91cf<\/p>\n<h3>2.2 \u53d8\u91cf\u7684\u57fa\u672c\u7c7b\u578b<\/h3>\n<p>\u5728Kotlin\u4e2d,\u53d8\u91cf\u6709\u5982\u4e0b\u7684\u57fa\u672c\u7c7b\u578b:<\/p>\n<ul>\n<li>\u6574\u578b: <code>Byte<\/code> <code>Short<\/code> <code>Int<\/code> <code>Long<\/code><\/li>\n<li>\u65e0\u7b26\u53f7\u6574\u5f62: <code>UByte<\/code> <code>UShort<\/code> <code>UInt<\/code> <code>ULong<\/code><\/li>\n<li>\u6d6e\u70b9\u578b: <code>Float<\/code> <code>Double<\/code><\/li>\n<li>\u5e03\u5c14\u578b:<code>Boolean<\/code><\/li>\n<li>\u5b57\u7b26\u578b: <code>Char<\/code><\/li>\n<li>\u5b57\u7b26\u4e32\u578b: <code>String<\/code><\/li>\n<\/ul>\n<p>\u6ce8\u610f:\u8fd9\u4e9b\u7c7b\u578b\u7684\u540d\u79f0\u90fd\u662f\u4ee5\u5927\u5199\u5f00\u5934\u7684<\/p>\n<h3>2.3 \u58f0\u660e\u53d8\u91cf<\/h3>\n<p>\u5728Kotlin\u4e2d:<\/p>\n<ul>\n<li>\u53d8\u91cf\u82e5\u662f\u5728\u58f0\u660e\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u88ab\u8d4b\u4e0a\u521d\u503c,\u5219\u4e0d\u9700\u8981\u58f0\u660e\u53d8\u91cf\u7684\u7c7b\u578b,\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5206\u914d\u53d8\u91cf\u7684\u7c7b\u578b<\/li>\n<li>\u82e5\u4ec5\u58f0\u660e\u800c\u4e0d\u8d4b\u503c,\u90a3\u4e48\u5fc5\u987b\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u7684\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">var name = &quot;Tangent0712&quot;\nval language:String\nlanguage = &quot;Kotlin&quot;<\/code><\/pre>\n<h3>2.4 \u53d8\u91cf\u7684\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<p><code>println()<\/code> \u51fd\u6570\u63a5\u6536\u7684\u662f\u5b57\u7b26\u4e32,\u5982\u679c\u6709\u53d8\u91cf\u53c2\u4e0e\u6253\u5370\u8f93\u51fa,\u5219\u5fc5\u987b\u628a\u53d8\u91cf\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">println(&quot;Hello!My name is ${name} !&quot;)<\/code><\/pre>\n<h2>3 \u5bb9\u5668<\/h2>\n<h3>3.1 \u5217\u8868(List)<\/h3>\n<p>\u5728Kotlin\u4e2d:<\/p>\n<ul>\n<li>\u5217\u8868\u4e2d\u7684\u6570\u636e\u662f\u6709\u5e8f\u7684<\/li>\n<li>\u5217\u8868\u7684\u6570\u636e\u7c7b\u578b\u4e3a`List\n`\u6216`MutableList`<\/li>\n<li><code>listOf()<\/code> \u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ea\u8bfb\u5217\u8868<\/li>\n<li><code>mutableListOf()<\/code> \u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ef\u53d8\u5217\u8868<\/li>\n<li>\u5217\u8868\u540d\u540e\u9762\u4f7f\u7528\u65b9\u62ec\u53f7\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20<\/li>\n<li><code>.first()<\/code> \u548c <code>.last()<\/code> \u540e\u7f00\u51fd\u6570\u8fd4\u56de\u5217\u8868\u4e2d\u9996\u4e2a\u6216\u672b\u4e2a\u5143\u7d20<\/li>\n<li><code>.count()<\/code> \u540e\u7f00\u51fd\u6570\u4ee5\u6574\u578b\u8fd4\u56de\u5217\u8868\u5143\u7d20\u7684\u4e2a\u6570<\/li>\n<li><code>in<\/code> \u5173\u952e\u5b57\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d,\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c<\/li>\n<li><code>.add(newItem)<\/code> \u548c <code>.remove(existingItem)<\/code> \u540e\u7f00\u51fd\u6570\u53ef\u4ee5\u5411\u5217\u8868\u7684\u672b\u5c3e\u65b0\u589e\u5143\u7d20\u6216\u4ece\u5217\u8868\u4e2d\u5220\u9664\u5df2\u6709\u5143\u7d20<\/li>\n<\/ul>\n<p>\u6ce8\u610f:<\/p>\n<ul>\n<li>\u53d8\u91cf\u7c7b\u578b\u4e2d\u7684 <code>val<\/code> \u548c <code>var<\/code> \u6307\u7684\u662f\u5f15\u7528\u7684\u53ef\u53d8\u6027,\u800c\u4e0d\u662f\u6307\u5f15\u7528\u5185\u5bb9\u7684\u53ef\u53d8\u6027<\/li>\n<li>\u4e5f\u5c31\u662f\u8bf4, <code>val<\/code> \u7c7b\u578b\u7684 <code>mutableList<\/code> \u4ecd\u7136\u53ef\u4ee5\u7f16\u8f91,\u4f46\u662f\u6574\u4e2a\u53d8\u91cf\u4e0d\u53ef\u4ee5\u6307\u5411\u4e00\u4e2a\u65b0\u7684\u5217\u8868<\/li>\n<\/ul>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">val myOC = mutableListOf(&quot;XiaoTing&quot;,&quot;LinLing&quot;)\n\/\/val means immutable reference,but the list content can be modified because we have used mutableListOf()\nmyOC.remove(&quot;XiaoTing&quot;)\nmyOC.add(&quot;XiaoTing&quot;)\nprintln(myOC)\n\/\/System:[LinLing, XiaoTing]<\/code><\/pre>\n<h3>3.2 \u96c6\u5408(Set)<\/h3>\n<p>\u5728Kotlin\u4e2d:<\/p>\n<ul>\n<li>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684,\u552f\u4e00\u7684,\u65e0\u6cd5\u6309\u7167\u7d22\u5f15\u83b7\u5f97\u5143\u7d20<\/li>\n<li>\u96c6\u5408\u7684\u6570\u636e\u7c7b\u578b\u4e3a`Set\n`\u6216`MutableSet`<\/li>\n<li>\u548c\u5217\u8868\u7c7b\u4f3c,\u96c6\u5408\u63d0\u4f9b<code>setOf()<\/code>\u548c<code>mutableSetOf()<\/code>\u51fd\u6570\u6765\u65b0\u5efa\u96c6\u5408<\/li>\n<li>\u548c\u5217\u8868\u7c7b\u4f3c,\u96c6\u5408\u63d0\u4f9b <code>in<\/code> \u5173\u952e\u5b57<\/li>\n<li>\u548c\u5217\u8868\u7c7b\u4f3c,\u96c6\u5408\u63d0\u4f9b<code>.add(newItem)<\/code> \u548c <code>.remove(existingItem)<\/code> \u540e\u7f00\u51fd\u6570,\u4ee5\u53ca\u7528\u6765\u8ba1\u6570\u7684 <code>.count()<\/code> \u540e\u7f00\u51fd\u6570<\/li>\n<\/ul>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">val myOC = muableSetOf(&quot;XiaoTing&quot;,&quot;LinLing&quot;)\nmyOC.remove(&quot;XiaoTing&quot;)\nmyOC.add(&quot;XiaoTing&quot;)\nprintln(&quot;XiaoTing&quot; in myOC)\n\/\/System:true<\/code><\/pre>\n<h3>3.3 \u5b57\u5178(Map)<\/h3>\n<p>\u5728Kotlin\u4e2d:<\/p>\n<ul>\n<li>\u5b57\u5178\u5b58\u50a8\u952e\u503c\u5bf9,\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684,\u952e\u503c\u5bf9\u662f\u65e0\u5e8f\u7684<\/li>\n<li>\u5b57\u5178\u7684\u6570\u636e\u7c7b\u578b\u4e3a <code>Map&lt;Key,Value&gt;<\/code> \u6216 <code>MutableMap&lt;Key,Value&gt;<\/code><\/li>\n<li>\u5728\u5b57\u5178\u540d\u540e\u9762\u4f7f\u7528\u65b9\u62ec\u53f7\u8f93\u5165\u952e\u53ef\u4ee5\u83b7\u53d6\u5bf9\u5e94\u7684\u503c,\u5982\u679c\u952e\u4e0d\u5b58\u5728,\u5219\u8fd4\u56de <code>null<\/code><\/li>\n<li>\u548c\u5217\u8868\u7c7b\u4f3c,\u5b57\u5178\u63d0\u4f9b<code>mapOf()<\/code>\u548c<code>mutableMapOf()<\/code>\u51fd\u6570\u6765\u65b0\u5efa\u5b57\u5178<\/li>\n<li>\u60f3\u8981\u5411\u5b57\u5178\u4e2d\u65b0\u589e\u952e\u503c\u5bf9,\u76f4\u63a5\u5c06\u503c\u8d4b\u7ed9\u5b57\u5178\u6240\u8bfb\u53d6\u7684\u76f8\u5e94\u7684\u952e\u5373\u53ef<\/li>\n<li><code>.containsKey(key)<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c,\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u76f8\u5e94\u7684\u952e<\/li>\n<li>\u4f7f\u7528<code>.keys<\/code>\u548c<code>.values<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u6216\u503c\u7684List<\/li>\n<\/ul>\n<pre><code class=\"lang-kotlin language-kotlin kotlin\">val myOC = mutableMapOf(&quot;young&quot; to &quot;XiaoTing&quot;)\nmyOC[&quot;adult&quot;] = &quot;LinLing&quot;\nmyOC.remove(&quot;young&quot;)<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>1 \u7b80\u5355\u7684Kotlin\u7a0b\u5e8f fun main() { println(&#8220;HelloWorld!&amp;qu [&hellip;]<\/p>","protected":false},"author":1,"featured_media":167,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin","category-coding"],"_links":{"self":[{"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":1,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions\/168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/media\/167"}],"wp:attachment":[{"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tangent0712.top\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}