{"id":1991,"date":"2024-09-18T02:40:19","date_gmt":"2024-09-17T18:40:19","guid":{"rendered":"https:\/\/blog.xiunian.wang\/?p=1991"},"modified":"2024-11-12T10:39:09","modified_gmt":"2024-11-12T02:39:09","slug":"kotlin-%e6%8b%93%e5%b1%95%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=1991","title":{"rendered":"Kotlin \u62d3\u5c55\u51fd\u6570"},"content":{"rendered":"\n<p>kotlin \u7684\u62d3\u5c55\u51fd\u6570\uff0c\u4e3a\u8fd9\u4e2a\u8bed\u8a00\u5e26\u6765\u6781\u5927\u7684\u53ef\u73a9\u6027\u3002\u53ef\u4ee5\u6781\u5927\u7684\u51cf\u5c11\u73b0\u6709\u7c7b\u7684\u5927\u5c0f, \u6bd4\u5982 string\u8fd9\u4e2a\u7c7b \u5728Android \u7684 java\u5b9e\u73b0\u4e2d \u6709\u4e09\u5343\u591a\u884c<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"886\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1024x886.png\" alt=\"\" class=\"wp-image-1992\" style=\"width:526px;height:auto\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1024x886.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-300x260.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-768x664.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1536x1329.png 1536w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image.png 1884w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u4f46\u662f\u5728 Kotlin \u7248\u672c\u4e2d\u7684 String\u7c7b\u53ea\u670950\u591a\u884c\uff0c\u4e0d\u5230\u4e00\u5c4f\u3002\u662f\u4e0d\u662f\u532a\u5937\u6240\u601d<\/p>\n\n\n\n<!--more-->\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"998\" height=\"1024\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1-998x1024.png\" alt=\"\" class=\"wp-image-1993\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1-998x1024.png 998w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1-292x300.png 292w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1-768x788.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1-1497x1536.png 1497w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-1.png 1534w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u90a3\uff0c\u6211\u4eec\u540c\u6837\u8c03\u7528\u7684\u90a3\u4e9b string \u7684\u65b9\u6cd5\u662f\u600e\u4e48\u505a\u5230\u65e0\u75db\u8c03\u7528\u7684\u5462\uff1f \u7b54\u6848\u5c31\u662f- \u51fd\u6570\u62d3\u5c55\u3002<\/p>\n\n\n\n<p>kotlin \u7684 string \u7684 function \u4e00\u90e8\u5206\u7684\u5b9e\u73b0\u90fd\u5728kotlin.text.Strings.kt\u4e2d\u4f5c\u4e3a\u62d3\u5c55\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8fd8\u6709\u4e00\u4e9b\u662f\u5728StringsJVM.kt \u4e2d\u5b9e\u73b0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"881\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2-1024x881.png\" alt=\"\" class=\"wp-image-1994\" style=\"width:570px;height:auto\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2-1024x881.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2-300x258.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2-768x660.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2-1536x1321.png 1536w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-2.png 1900w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u5229\u7528\u62d3\u5c55\u51fd\u6570\u4e5f\u80fd\u5b9e\u73b0\u57fa\u672c\u7684\u62d3\u5c55\u65b9\u6cd5\uff0c\u4e5f\u80fd\u4e3a\u73b0\u6709\u7684\u7c7b\u65b0\u589e\u65b0\u7684\u65b9\u6cd5\uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9876\u5c42\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8e\u9759\u6001\u65b9\u6cd5\uff0c\u5230\u5904\u90fd\u80fd\u5f15\u7528\uff09\u6bd4\u5982\u4e3a\u5b57\u7b26\u4e32\u589e\u52a0\u4e00\u4e2a\u5224\u65ad\u662f\u5426\u662f\u90ae\u7bb1\u7684\u62d3\u5c55\u65b9\u6cd5\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u81ea\u5b9a\u4e49 String \u7c7b\u7684\u6269\u5c55\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u662f\u5426\u662f\u5408\u6cd5\u7684\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\nfun String.isValidEmail(): Boolean {\n    \/\/ \u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\n    val emailRegex = \"^[A-Za-z](.*)([@])(.+)(\\\\.)(.+)\".toRegex()\n    return this.matches(emailRegex)\n}\n\nfun main() {\n    val email = \"example@example.com\"\n    val invalidEmail = \"example.com\"\n\n    \/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6269\u5c55\u51fd\u6570\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u5408\u6cd5\u7684\u7535\u5b50\u90ae\u4ef6\n    println(email.isValidEmail()) \/\/ \u8f93\u51fa: true\n    println(invalidEmail.isValidEmail()) \/\/ \u8f93\u51fa: false\n}<\/pre>\n\n\n\n<p>\u4f46\u662f\u4e5f\u6709\u5c40\u9650\u6027\uff0c\u5c31\u662f\u6269\u5c55\u51fd\u6570\u4e0d\u80fd\u91cd\u5199\u7236\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u4e14\u5b83\u4eec\u5728\u7ee7\u627f\u7ed3\u6784\u4e2d\u4e0d\u4f1a\u88ab\u5b50\u7c7b\u8986\u76d6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">open class Parent\nclass Child : Parent()\n\nfun Parent.foo() = \"Parent\"\nfun Child.foo() = \"Child\"\n\nfun main() {\n    val p: Parent = Child()\n    println(p.foo())  \/\/ \u8f93\u51fa: \"Parent\" \u800c\u4e0d\u662f \"Child\"\n}<\/pre>\n\n\n\n<p>\u62d3\u5c55\u51fd\u6570\u4e5f\u80fd\u62d3\u5c55\u5c5e\u6027\uff0c\u4f46\u672c\u8d28\u4e0a\u53ea\u662f\u4e00\u4e2a get\/set \u65b9\u6cd5\uff0c\u4e0d\u4f1a\u771f\u6b63\u4fee\u6539\u5bf9\u8c61\u7684\u5185\u5b58\u7ed3\u6784\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">val String.firstChar: Char\n    get() = this[0]\n\nfun main() {\n    val str = \"Hello\"\n    println(str.firstChar)  \/\/ \u8f93\u51fa: 'H'\n}<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u673a\u5236\u8ba9 Kotlin \u65e2\u4fdd\u6301\u4e86\u4e0e Java \u7684\u517c\u5bb9\u6027\uff0c\u540c\u65f6\u53c8\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u7c7b\u7684\u60c5\u51b5\u4e0b\u4e3a\u5176\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6b63\u513f\u516b\u7ecf\u7684\u4e8b\u60c5\u8bf4\u5b8c\u4e86\uff0c\u4f46\u662f\u4eca\u5929\u8981\u4ecb\u7ecd\u7684\u662f\u4e00\u4e9b\u5947\u6280\u6deb\u5de7\u3002\u4e0b\u9762\u5f00\u59cb<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.\u89e3\u6784(destructure)\u6570\u636e<\/h3>\n\n\n\n<p>\u4ec0\u4e48\u662f\u89e3\u6784\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u5bf9\u62ec\u53f7\uff0c\u653e\u5165\u6211\u4eec\u7684\u53d8\u91cf\uff0c\u7136\u540e\u76f4\u63a5<code>=<\/code>\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c31\u662f\u4e00\u6bb5\u89e3\u6784\u58f0\u660e\u3002<strong>Kotlin<\/strong>\u4f1a\u81ea\u52a8\u6309\u7167\u7c7b\u53d8\u91cf\u58f0\u660e\u7684\u987a\u5e8f\uff0c\u5c06\u76f8\u5173\u7684\u503c\u53d6\u51fa\u8d4b\u503c\u7ed9<code>=<\/code>\u5de6\u8fb9\u7684\u53d8\u91cf\u3002\u770b\u4e00\u4e0b\u5982\u4e0b\u4f8b\u5b50\u5c31\u61c2\u4e86<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">data class User(val id:Int, val name:String)\nfun main() {\n    val user = User(10086, \"hugh\")\n    val (uid, name) = user\n    println(uid)\/\/10086\n    println(name)\/\/hugh\n    println(user.component1())\/\/10086\n    println(user.component2())\/\/hugh\n\n }<\/pre>\n\n\n\n<p>\u8fd8\u53ef\u4ee5\u5728 map \u4e2d\u4f7f\u7528\uff0c\u53ef\u80fd\u904d\u5386\u4e00\u4e2a\u6620\u5c04\uff08map\uff09\u6700\u597d\u7684\u65b9\u5f0f\u5c31\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for ((key, value) in map) {\n   \/\/ \u4f7f\u7528\u8be5 key\u3001value \u505a\u4e9b\u4e8b\u60c5\n}<\/pre>\n\n\n\n<p>\u9996\u5148\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462\uff1f \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u6269\u5c55\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">operator fun &lt;K, V> Map&lt;K, V>.iterator(): Iterator&lt;Map.Entry&lt;K, V>> = entrySet().iterator()\noperator fun &lt;K, V> Map.Entry&lt;K, V>.component1() = getKey()\noperator fun &lt;K, V> Map.Entry&lt;K, V>.component2() = getValue()<\/pre>\n\n\n\n<p>\u4e0a\u9762\u5bf9\u5e94\u7684\u62d3\u5c55\u4e86\u64cd\u4f5c\u7b26 for\/\u51fd\u6570\u540diterator   \u548c\u64cd\u4f5c\u7b26 ()\/ \u89e3\u6784\u58f0\u660e component1<\/p>\n\n\n\n<p>\u6240\u4ee5\u4f7f\u7528 component \u6211\u4eec\u53ef\u4ee5\u5bf9\u4efb\u4f55\u6570\u636e\u8fdb\u884c\u89e3\u6784\uff0c\u6bd4\u5982\u8981\u60f3\u5b9e\u73b0\u5982\u4e0b\u6570\u636e<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"206\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3-1024x206.png\" alt=\"\" class=\"wp-image-1995\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3-1024x206.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3-300x60.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3-768x155.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3-1536x309.png 1536w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/09\/image-3.png 1670w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u6839\u636e\u63d0\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u62d3\u5c55 LocalDate \u7c7b\u7684 \u64cd\u4f5c\u7b26 component1\uff0ccomponent2\uff0ccomponent3<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">operator fun LocalDate.component1()= year\noperator fun LocalDate.component2()= monthValue\noperator fun LocalDate.component3()= dayOfMonth<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u80fd\u4f7f\u7f16\u8bd1\u901a\u8fc7\uff0c\u5e76\u4e14\u6b63\u786e\u6267\u884c\u4e86\u3002\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u5b9a\u4e49\u6210 data class<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5b57\u7b26\u4e32\u4e5f\u53d8\u6210\u51fd\u6570\uff1f\/invoke<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  \"hello world\"()<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u62d3\u5c55\u4e00\u4e2a\u5bf9\u8c61\u7684invoke\u65b9\u6cd5\uff0c\u6765\u5b9e\u73b0\u5bf9\u5bf9\u8c61\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">operator fun Any?.invoke() {\n    println(this)\n}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u53ef\u4ee5\u8ba9\u4efb\u610f\u5bf9\u8c61\u53d8\u6210\u4e00\u4e2a\u51fd\u6570\u8c03\u7528,\u90fd\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \"hello world\"()\n    1()\n    null()\n    true()<\/pre>\n\n\n\n<p>\u4e8e\u662f\uff0c\u4fbf\u53ef\u4ee5\u5b9e\u73b0\u5982\u4e0b\u6548\u679c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    val exp = 3(2 + 6)\n    println(exp)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u901a\u8fc7\u7d22\u5f15\u6765\u627e\u76ee\u6807\/ get<\/h2>\n\n\n\n<p>\u4f60\u5e94\u8be5\u5199\u8fc7\u5982\u4e0b\u7684\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    val list = arrayListOf(1, 2, 3)\n    println(list[1])<\/pre>\n\n\n\n<p>\u5982\u679c\u60f3\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u4ece\u4e00\u4e2a\u6570\u5b57\u4e2d\uff0c\u627e\u5230\u7b2c\u51e0\u4f4d\uff0c\u6bd4\u5982\u4ece 321 \u4e2d\u63d0\u53d6\u51fa\u4e2a\u4f4d\uff0c\u5341\u4f4d\uff0c\u767e\u4f4d\u5982\u4e0b\u4ee3\u7801\u662f\u4e0d\u662f\u770b\u8d77\u6765\u5f88\u7b80\u6d01<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    val count = 123\n    println(count[3])\/\/0\n    println(count[2])\/\/1\n    println(count[1])\/\/2<\/pre>\n\n\n\n<p>\u5b83\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u91cd\u8f7d\u64cd\u4f5c\u7b26\uff0c\u8ba9\u4e0a\u9762\u8868\u8fbe\u5f0f\u751f\u6548<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">operator fun Int.get(idx: Int): Int = div(10.0.pow(idx.toDouble())).rem(10).toInt()<\/pre>\n\n\n\n<p>\u9664\u4e86\u5bf9\u53d8\u91cf\u8fdb\u884c\u62d3\u5c55 get \u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u5bf9\u51fd\u6570\u8868\u8fbe\u5f0f\u8fdb\u884c\u62d3\u5c55<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> val double: (Int) -> Int = { it * 2 }\n println(double(12))<\/pre>\n\n\n\n<p>\u8ba9\u8fd9\u4e2a\u53d8\u91cf\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5947\u602a\u7684\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    operator fun ((Int) -> Int).get(idx:Int):Int{\n        return this(idx)\n    }\n    println(double[12])\/\/24\n    operator fun ((Int) -> Int).invoke(call: () -> Int): Int {\n        return this(call())\n    }\n    print(double { 12 })\/\/24<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.  \u50cf\u81ea\u7136\u8bed\u8a00\u4e00\u6837\u5199\u8bed\u53e5 \/contains,in<\/h2>\n\n\n\n<p>\u56de\u5230\u4e4b\u524d\u7684\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    val data = LocalDate.parse(\"2024-09-17\")<\/pre>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5224\u65ad\u8fd9\u4e2a\u65e5\u671f\u5728\u4e0d\u5728\u54ea\u4e00\u5e74\u54ea\u4e00\u6708,\u53ef\u4ee5\u7528\u5982\u4e0b\u8bed\u53e5\u5224\u65ad<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> operator fun Month.contains(date: LocalDate): Boolean {\n        return this == date.month\n    }\n\n    operator fun Int.contains(date: LocalDate): Boolean {\n        return this == date.year\n    }\n\n    infix fun Month.of(year: Int): Pair&lt;Month, Int> = this to year \/\/\u4e2d\u7f00\u51fd\u6570\n\n    operator fun Pair&lt;Month, Int>.contains(date: LocalDate): Boolean {\n        return this.first == date.month &amp;&amp; this.second == date.year\n    }\n    println(data in Month.OCTOBER)\/\/false\n    println(data in 2024)\/\/true\n    println(data in Month.SEPTEMBER of 2024)\/\/true\n    val p = Month.MAY of 2023<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u62d3\u5c55\u4e86 \u5e74\u6708\u4efd\u7684\u5224\u65ad contains,  \u4f7f\u7528\u4e2d\u7f00\u51fd\u6570\uff0c\u589e\u52a0\u4e86  of \u6765\u8fd4\u56de\u4e00\u4e2a pair \u518d\u62d3\u5c55 pair \u7684contains \u65b9\u6cd5\u5c31\u53ef\u4ee5\u8fbe\u5230 \u50cf\u82f1\u8bed\u4e00\u6837\u5199\u8868\u8fbe\u5f0f\u4e86\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7ed3\u8bed<\/h1>\n\n\n\n<p>Kotlin \u4e2d\u63d0\u4f9b\u4e86\u4e00\u5957\u5e38\u89c1\u7684\u53ef\u91cd\u8f7d\u64cd\u4f5c\u7b26\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<p>\u5728 Kotlin \u4e2d\uff0c<code>operator<\/code> \u5173\u952e\u5b57\u7528\u4e8e<strong>\u91cd\u8f7d\u64cd\u4f5c\u7b26<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7c7b\u7684\u67d0\u4e9b\u64cd\u4f5c\u7b26\u884c\u4e3a\uff0c\u4f7f\u5f97\u4f60\u7684\u7c7b\u53ef\u4ee5\u50cf\u57fa\u7840\u7c7b\u578b\u4e00\u6837\u4f7f\u7528\u64cd\u4f5c\u7b26\uff08\u4f8b\u5982 <code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>[]<\/code>, <code>++<\/code>, <code>--<\/code> \u7b49\uff09\u3002<\/p>\n\n\n\n<p>Kotlin \u4e2d\u7684\u64cd\u4f5c\u7b26\u91cd\u8f7d\u5e76\u4e0d\u662f\u4fee\u6539\u73b0\u6709\u7684\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e9b\u7279\u6b8a\u7684\u51fd\u6570\uff08\u901a\u5e38\u5e26\u6709 <code>operator<\/code> \u5173\u952e\u5b57\uff09\uff0c\u8ba9\u4f60\u80fd\u591f\u4e3a\u81ea\u5b9a\u4e49\u7684\u7c7b\u8d4b\u4e88\u7279\u5b9a\u7684\u64cd\u4f5c\u7b26\u884c\u4e3a\u3002Kotlin \u63d0\u4f9b\u4e86\u4e00\u5957\u53ef\u4ee5\u91cd\u8f7d\u7684\u64cd\u4f5c\u7b26\u5217\u8868\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u7b26\u90fd\u6709\u5176\u5bf9\u5e94\u7684\u51fd\u6570\u540d\u79f0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c1\u7684\u53ef\u91cd\u8f7d\u64cd\u4f5c\u7b26<\/h3>\n\n\n\n<p>Kotlin \u4e2d\u63d0\u4f9b\u4e86\u4e00\u5957\u5e38\u89c1\u7684\u53ef\u91cd\u8f7d\u64cd\u4f5c\u7b26\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u5bf9\u5e94\u51fd\u6570\u540d<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td><code>plus<\/code><\/td><td><code>a + b<\/code><\/td><\/tr><tr><td><code>-<\/code><\/td><td><code>minus<\/code><\/td><td><code>a - b<\/code><\/td><\/tr><tr><td><code>*<\/code><\/td><td><code>times<\/code><\/td><td><code>a * b<\/code><\/td><\/tr><tr><td><code>\/<\/code><\/td><td><code>div<\/code><\/td><td><code>a \/ b<\/code><\/td><\/tr><tr><td><code>%<\/code><\/td><td><code>rem<\/code> \u6216 <code>mod<\/code><\/td><td><code>a % b<\/code><\/td><\/tr><tr><td><code>++<\/code><\/td><td><code>inc<\/code><\/td><td><code>a++<\/code><\/td><\/tr><tr><td><code>--<\/code><\/td><td><code>dec<\/code><\/td><td><code>a--<\/code><\/td><\/tr><tr><td><code>[]<\/code><\/td><td><code>get<\/code> \/ <code>set<\/code><\/td><td><code>a[i]<\/code> \/ <code>a[i] = value<\/code><\/td><\/tr><tr><td><code>==<\/code><\/td><td><code>equals<\/code><\/td><td><code>a == b<\/code><\/td><\/tr><tr><td><code>&gt;<\/code><\/td><td><code>compareTo<\/code><\/td><td><code>a &gt; b<\/code><\/td><\/tr><tr><td><code>in<\/code><\/td><td><code>contains<\/code><\/td><td><code>a in b<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>kotlin \u7684\u62d3\u5c55\u51fd\u6570\uff0c\u4e3a\u8fd9\u4e2a\u8bed\u8a00\u5e26\u6765\u6781\u5927\u7684\u53ef\u73a9\u6027\u3002\u53ef\u4ee5\u6781\u5927\u7684\u51cf\u5c11\u73b0\u6709\u7c7b\u7684\u5927\u5c0f, \u6bd4\u5982 string\u8fd9\u4e2a\u7c7b \u5728Android \u7684 java\u5b9e\u73b0\u4e2d \u6709\u4e09\u5343\u591a\u884c \u4f46\u662f\u5728 Kotlin \u7248\u672c\u4e2d\u7684 String\u7c7b\u53ea\u670950\u591a\u884c\uff0c\u4e0d\u5230\u4e00\u5c4f\u3002\u662f\u4e0d\u662f\u532a\u5937\u6240\u601d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1991"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1991"}],"version-history":[{"count":3,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1991\/revisions"}],"predecessor-version":[{"id":1999,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1991\/revisions\/1999"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}