{"id":92,"date":"2017-08-01T20:45:24","date_gmt":"2017-08-01T12:45:24","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=92"},"modified":"2021-07-18T20:25:23","modified_gmt":"2021-07-18T12:25:23","slug":"%e9%97%9c%e6%96%bckotlin","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=92","title":{"rendered":"[Android]Kotlin\u5b78\u7fd2\u7b46\u8a18#1-\u8a9e\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5ba3\u544a\u90e8\u5206<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u6578\u5ba3\u544a<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u5f80\u6211\u5011\u5728\u5bebJava\u7684\u6642\u5019\uff0c\u5982\u679c\u8981\u5ba3\u544a\u4e00\u500b\u5e38\u6578\uff0c\u5247\u8981\u5728\u578b\u5225\u524d\u9762\u52a0\u4e0a<code>final<\/code>\u3002<\/p>\n\n\n\n<p>kotlin\u5247\u662f\u7528<code>val<\/code>\u5ba3\u544a\u70ba\u5e38\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">val text: String = \"\u6e2c\u8a66\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8b8a\u6578\u5ba3\u544a<\/strong><\/h3>\n\n\n\n<p>\u5728Java\uff0c\u578b\u5225\u524d\u9762\u6c92\u6709\u52a0\u4e0a<code>final<\/code>\u5373\u70ba\u8b8a\u6578\u3002\u4f46\u5728Kotlin\u5247\u662f\u7528var\u5ba3\u544a\u70ba\u8b8a\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">\/\/ \u5ba3\u544a x \u70ba\u8b8a\u6578\uff0c\u521d\u59cb\u503c\u70ba 0\nvar x: Int = 0\n\n\/\/ x + 10\nx += 10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u51fd\u6578\u5ba3\u544a<\/strong><\/h3>\n\n\n\n<p>\u5728Kotlin\uff0c\u5ba3\u544a\u4e00\u500bfunction\u6642\uff0c\u95dc\u9375\u5b57\u70bafun\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun functionA() {\n    \/\/ Do something...\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5e0c\u671bfunction\u547c\u53eb\u5b8c\u56de\u50b3\u7d50\u679c\uff0c\u53ea\u9700\u5728function\u5ba3\u544a\u884c\u6578\u5f8c\u9762\u52a0\u5165\u5192\u865f\uff0c\u4e26\u52a0\u4e0a\u5e0c\u671b\u56de\u50b3\u7684\u578b\u5225\u5373\u53ef\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">fun functionWithReturn(): String {\n    \/\/ Do something...\n    return \"Return Value\"\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u4e32\u6a21\u677f<\/h2>\n\n\n\n<p>\u4ee5\u5f80\u6211\u5011\u5728\u64b0\u5bebJava\u7a0b\u5f0f\u78bc\u6642\uff0c\u8981\u5c07\u8b8a\u6578\u7d44\u5408\u5230\u5b57\u4e32\u4e2d\u6642\uff0c\u6703\u4f7f\u7528StringBuilder\u6216\u662f\u76f4\u63a5\u4f7f\u7528+\u865f\u4f86\u7d44\u5408\u5b57\u4e32\u3002<\/p>\n\n\n\n<p>\u5728Kotlin\u4e2d\uff0c\u63d0\u4f9b\u4e86\u5b57\u4e32\u6a21\u677f\u9019\u500b\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u5b57\u4e32\u4e2d\u4f7f\u7528$\u5c07\u8b8a\u6578\u6578\u503c\u7d44\u5408\u5230\u5b57\u4e32\u4e2d\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">var price = 100\nprintln(\"\u9019\u500b\u6771\u897f\u7684\u50f9\u683c\u662f\uff1a $price \u5143\")<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Elvis\u904b\u7b97\u5b50<\/h2>\n\n\n\n<p>Elvis\u5176\u5be6\u5c31\u662f\u7a7a\u503c\u6aa2\u6e2c\u7684\u904b\u7b97\u5b50\uff0c\u4ee5?:\u8868\u793a\u3002\u7576\u6578\u503c\u70ba\u7a7a\u6642\uff0c\u6703\u4ee5?:\u53f3\u65b9\u7684\u6578\u503c\u4f5c\u70ba\u904b\u7b97\u7684\u7d50\u679c\u8f38\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">fun main(args: Array&lt;String>) {\n    var strA: String? = null\n    val value = strA ?: \"ABCDEFG\"\n    pribtln(\"$value\")\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u76ee\u524d\u5c0dKotlin\u8a9e\u6cd5\u7814\u7a76\u7684\u4e00\u4e9b\u7c21\u55ae\u7d00\u9304\uff0c\u82e5\u4e4b\u5f8c\u6709\u7814\u7a76\u51fa\u5176\u4ed6\u65b0\u7684\u8a9e\u6cd5\u7528\u6cd5\u6703\u518d\u88dc\u5145\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5ba3\u544a\u90e8\u5206 \u5e38\u6578\u5ba3\u544a \u4ee5\u5f80\u6211\u5011\u5728\u5bebJava\u7684\u6642\u5019\uff0c\u5982\u679c\u8981\u5ba3\u544a\u4e00\u500b\u5e38\u6578\uff0c\u5247\u8981\u5728\u578b\u5225\u524d\u9762\u52a0\u4e0afinal\u3002 kotli &hellip; <\/p>\n","protected":false},"author":1,"featured_media":113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,19],"class_list":["post-92","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-android-app","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92"}],"version-history":[{"count":3,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions\/992"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}