{"id":1297,"date":"2022-11-05T21:00:01","date_gmt":"2022-11-05T13:00:01","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1297"},"modified":"2024-03-21T17:02:48","modified_gmt":"2024-03-21T09:02:48","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0-%e6%a0%87%e5%87%86%e5%87%bd%e6%95%b0%e5%92%8c%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/11\/05\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0-%e6%a0%87%e5%87%86%e5%87%bd%e6%95%b0%e5%92%8c%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60-\u6807\u51c6\u51fd\u6570\u548c\u9759\u6001\u65b9\u6cd5"},"content":{"rendered":"\n<p>Kotlin\u7684\u6807\u51c6\u51fd\u6570\u6307\u7684\u662fStandard.kt\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u4efb\u4f55Kotlin\u4ee3\u7801\u90fd\u53ef\u4ee5\u81ea\u7531\u5730\u8c03\u7528\u6240\u6709\u7684\u6807\u51c6\u51fd\u6570\u3002\u5176\u6700\u4e3b\u8981\u4f7f\u7528\u7684\u662fwith\u3001run\u548capply\u8fd9\u4e09\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>with\u51fd\u6570\u4f1a\u5728Lambda\u8868\u8fbe\u5f0f\u4e2d\u63d0\u4f9b\u7b2c\u4e00\u4e2a\u53c2\u6570\u5bf9\u8c61\u7684\u4e0a\u4e0b\u6587\uff0c\u5e76\u4f7f\u7528Lambda\u8868\u8fbe\u5f0f\u4e2d\u7684\u6700\u540e\u4e00\u884c\u4ee3\u7801\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val result = with(obj) {\n    \/\/ \u8fd9\u91cc\u662fobj\u7684\u4e0a\u4e0b\u6587\n    \"value\" \/\/ with\u51fd\u6570\u7684\u8fd4\u56de\u503c\n}\n\/\/\u5177\u4f53\u4f8b\u5b50\n\/\/\u6253\u5370\u6c34\u679c\nval list = listOf(\"Apple\", \"Banana\", \"Orange\", \"Pear\", \"Grape\")\nval result = with(StringBuilder()) {\n     append(\"Start eating fruits.\\n\")\n     for (fruit in list) {\n         append(fruit).append(\"\\n\")\n     }\n     append(\"Ate all fruits.\")\n     toString()\n}\nprintln(result)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u5728with\u5185\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61StringBuilder()\uff0c\u4e4b\u540ewith\u5185\u65b9\u6cd5\u76f8\u5f53\u4e8e\u4f9d\u6b21\u8c03\u7528\u4e86StringBuilder().append(), \u5faa\u73afStringBuilder().append()\u7b49\u7b49\uff0c\u4e4b\u540e\u901a\u8fc7\u4e00\u4e2a\u6700\u540e\u4e00\u884c\u7684StringBuilder().toString()\u8fd4\u56de\u4e86\u6574\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<div style=\"height:15px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e4b\u540e\u662frun\u51fd\u6570\uff0c\u4e0ewith\u7c7b\u4f3c\uff0c\u4f46\u786e\u662f\u5efa\u7acb\u5728\u67d0\u4e2a\u5bf9\u8c61\u4e0a\u7684\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val result = obj.run {\n    \/\/ \u8fd9\u91cc\u662fobj\u7684\u4e0a\u4e0b\u6587\n    \"value\" \/\/ run\u51fd\u6570\u7684\u8fd4\u56de\u503c\n}\n\/\/\u5177\u4f53\u4f8b\u5b50\n\/\/\u6253\u5370\u6c34\u679c\nval list = listOf(\"Apple\", \"Banana\", \"Orange\", \"Pear\", \"Grape\")\nval result = StringBuilder().run {\n    append(\"Start eating fruits.\\n\")\n    for (fruit in list) {\n       append(fruit).append(\"\\n\")\n    }\n    append(\"Ate all fruits.\")\n    toString()\n}\nprintln(result)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\uff0crun\u4e0ewith\u7684\u4e0d\u540c\u70b9\u5728\u4e8e\uff0crun\u4e0d\u9700\u8981\u5f15\u5165\u53c2\u6570\u800c\u662f\u76f4\u63a5\u4f5c\u7528\u4e8e\u5bf9\u8c61\u3002<\/p>\n\n\n\n<div style=\"height:8px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6700\u540e\u662fapply\uff0c\u5b83\u4e0erun\u4f7f\u7528\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u4e0d\u540c\u4e8erun\u548cwith\uff0c\u4e0d\u4f1a\u5c06\u6700\u540e\u4e00\u884c\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u800c\u662f\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\u672c\u8eab\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val result = obj.apply {\n    \/\/ \u8fd9\u91cc\u662fobj\u7684\u4e0a\u4e0b\u6587\n}\n\/\/ result == obj\n\/\/\u5177\u4f53\u4f8b\u5b50\n\/\/\u6253\u5370\u6c34\u679c\nval list = listOf(\"Apple\", \"Banana\", \"Orange\", \"Pear\", \"Grape\")\nval result = StringBuilder().apply {\n     append(\"Start eating fruits.\\n\")\n     for (fruit in list) {\n         append(fruit).append(\"\\n\")\n     }\n     append(\"Ate all fruits.\")\n}\nprintln(result.toString())<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\uff0capply\u4e4b\u540e\u6ca1\u6709\u518d\u4f7f\u7528toString()\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5bf9\u8c61\u8fd4\u56de\u7684\u8c03\u7528\u5bf9\u8c61\u672c\u8eab\uff0c\u6240\u4ee5toString()\u6ca1\u7528\uff0c\u800c\u662f\u5728\u4e4b\u540e\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u719f\u7ec3\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u80fd\u591f\u66f4\u5feb\u5730\u5b8c\u6210\u6570\u636e\u7684\u89e3\u6790\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div style=\"height:14px\" 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\">\u9759\u6001\u65b9\u6cd5<\/p>\n\n\n\n<p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u9759\u6001\u65b9\u6cd5\u662f\u5f88\u91cd\u8981\u7684\u4e00\u73af\u3002\u4f46Kotlin\u5f31\u5316\u4e86\u9759\u6001\u65b9\u6cd5\u8fd9\u4e2a\u6982\u5ff5\uff0c\u56e0\u4e3akotlin\u63d0\u4f9b\u4e86\u6bd4\u9759\u6001\u65b9\u6cd5\u66f4\u597d\u7684\u8bed\u6cd5\u7279\u6027&#8211;\u5355\u4f8b\u7c7b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">object Util {\n    fun doAction() {\n        println(\"do action\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728Kotlin\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u5355\u4f8b\u7c7b\u5185\u521b\u5efa\u65b9\u6cd5\uff0c\u5e76\u5728\u4e4b\u540e\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528 object.method()\u5982\u540c\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u7684\u6837\u5b50\u6765\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4f46\u5982\u679c\u6211\u4eec\u5355\u7eaf\u60f3\u8981\u67d0\u4e9b\u65b9\u6cd5\u53d8\u6210\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u683c\u5f0f\u5462\uff1f\u53ef\u4ee5\u4f7f\u7528companion object{}\u6765\u5b8c\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class Util {\n    fun doAction1() {\n        println(\"do action1\")\n    }\n    companion object {\n        fun doAction2() {\n            println(\"do action2\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e0d\u8fc7\uff0cdoAction2()\u65b9\u6cd5\u5176\u5b9e\u4e5f\u5e76\u4e0d\u662f\u9759\u6001\u65b9\u6cd5\uff0ccompanion object\u8fd9\u4e2a\u5173\u952e\u5b57\u5b9e\u9645\u4e0a\u4f1a\u5728Util\u7c7b\u7684\u5185\u90e8\u521b\u5efa\u4e00\u4e2a\u4f34\u751f\u7c7b\uff0c\u800cdoAction2()\u65b9\u6cd5\u5c31\u662f\u5b9a\u4e49\u5728\u8fd9\u4e2a\u4f34\u751f\u7c7b\u91cc\u9762\u7684\u5b9e\u4f8b\u65b9\u6cd5\u3002\u53ea\u662fKotlin\u4f1a\u4fdd\u8bc1Util\u7c7b\u59cb\u7ec8\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a\u4f34\u751f\u7c7b\u5bf9\u8c61\uff0c\u56e0\u6b64\u8c03\u7528Util.doAction2()\u65b9\u6cd5\u5b9e\u9645\u4e0a\u5c31\u662f\u8c03\u7528\u4e86Util\u7c7b\u4e2d\u4f34\u751f\u5bf9\u8c61\u7684doAction2()\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4f46\u4e0a\u8bc9\u65b9\u6cd5\u672c\u8d28\u4e0a\u5e76\u4e0d\u662f\u9759\u6001\u65b9\u6cd5\uff0c\u5f53\u6211\u4eec\u5728java\u4e2d\u4ee5\u9759\u6001\u65b9\u6cd5\u7684\u5f62\u5f0f\u8c03\u7528\u8fd9\u4e9b\u7c7b\u65f6\uff0c\u4f1a\u53d1\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u4e0d\u5b58\u5728\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u8981\u771f\u6b63\u5c06\u65b9\u6cd5\u53d8\u6210\u9759\u6001\u65b9\u6cd5\uff0c\u9700\u8981\u52a0\u5165\u6ce8\u89e3-@JvmStatic\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">companion object {\n    @JvmStatic\n    fun doAction2() {\n        println(\"do action2\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u7684\u662f\uff0c@JvmStatic\u6ce8\u89e3\u53ea\u80fd\u52a0\u5728\u5355\u4f8b\u7c7b\u6216companion object\u4e2d\u7684\u65b9\u6cd5\u4e0a\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u52a0\u5728\u4e00\u4e2a\u666e\u901a\u65b9\u6cd5\u4e0a\uff0c\u4f1a\u76f4\u63a5\u63d0\u793a\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u6dfb\u52a0\u6ce8\u89e3\u6765\u5b9e\u73b0\u9759\u6001\u65b9\u6cd5\uff0cKotlin\u4e5f\u80fd\u4f7f\u7528\u9876\u5c42\u65b9\u6cd5\u6765\u5b9e\u73b0\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u9876\u5c42\u65b9\u6cd5\u6307\u7684\u662f\u90a3\u4e9b\u6ca1\u6709\u5b9a\u4e49\u5728\u4efb\u4f55\u7c7b\u4e2d\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Kotlin\u6587\u4ef6\uff0c\u5e76\u5728\u5185\u90e8\u76f4\u63a5\u5199\u5165\u65b9\u6cd5\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u9876\u5c42\u65b9\u6cd5\u3002Kotlin\u7f16\u8bd1\u5668\u4f1a\u5c06\u6240\u6709\u7684\u9876\u5c42\u65b9\u6cd5\u5168\u90e8\u7f16\u8bd1\u6210\u9759\u6001\u65b9\u6cd5\uff0c\u800c\u8c03\u7528\u53ea\u9700\u8981\u7c7b\u540d+\u9876\u5c42\u65b9\u6cd5\u540d\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin\u7684\u6807\u51c6\u51fd\u6570\u6307\u7684\u662fStandard.kt\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u4efb\u4f55Kotlin\u4ee3\u7801\u90fd\u53ef\u4ee5\u81ea\u7531\u5730\u8c03\u7528\u6240\u6709\u7684 [&hellip;]<\/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-1297","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\/1297"}],"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=1297"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1297\/revisions"}],"predecessor-version":[{"id":1298,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1297\/revisions\/1298"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}