{"id":698,"date":"2019-12-02T09:15:04","date_gmt":"2019-12-02T00:15:04","guid":{"rendered":"https:\/\/www.itchefblog.com\/?p=698"},"modified":"2019-12-30T23:10:21","modified_gmt":"2019-12-30T14:10:21","slug":"kotlin-basics","status":"publish","type":"post","link":"https:\/\/www.itchefblog.com\/?p=698","title":{"rendered":"Kotlin Basics"},"content":{"rendered":"<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\uc22b\uc790 \ud615\ud0dc\uc758 \ubb38\uc790\uc5f4\uc744 \uc22b\uc790\ub85c \ubc14\uafc0 \ub54c\r\nval intStr = &quot;10&quot;\r\nval str = Integer.patseInt(intStr)\r\n\r\n\/\/\uc77c\ubc18 \ud074\ub798\uc2a4\uac04\uc5d0 \ud615\ubcc0\ud658 \uc2dc as \uc0ac\uc6a9\r\nopen class Animal\r\nclass Dog : Animal()\r\nval dog = Dog()\r\nval animal = dog as Animal\r\n\r\n\/\/\uc22b\uc790\ud615 \uc790\ub8cc\ub07c\ub9ac \ud615 \ubcc0\ud658\r\nval a = 10L\r\nval b = 20\r\n\r\nval c = a.toInt()\r\nval d = b.toDouble()\r\nval e = a.toString()\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\ud615 \uccb4\ud06c, JAVA\uc758 instanceOf \uc5d0 \ud574\ub2f9\u3148\r\nval str = &quot;hello&quot;\r\n\r\nif(str is String){\r\n\tprintln(str.toUpperCase())\r\n}\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/let() \ud568\uc218\r\n\/\/fun &lt;T, R&gt; T.let(block: (T) -&gt; R): R\r\nval result = str?.let {\r\n\tInteger.parseInt(it)\r\n}\r\n\r\n\/\/let \ud568\uc218\ub294 \ube14\ub85d\uc5d0 \uc790\uae30 \uc790\uc2e0\uc744 \uc778\uc218\ub85c \uc804\ub2ec\ud558\uace0 \uc218\ud589\ub41c \uacb0\uacfc\ub97c \ubc18\ud658\ud55c\ub2e4.\r\n\/\/\uc778\uc218\ub85c \uc804\ub2ec\ud55c \uaca9\uccb4\ub294 it\uc73c\ub85c \ucc38\uc870\ud558\uba70, let\uc744 ?.\uacfc \ud568\uaf10 \uc0ac\uc6a9\ud558\uba74 null\uac12\uc774 \uc544\ub2d0 \ub54c\ub9cc \uc2e4\ud589\ud558\ub294 \ucf54\ub4dc\ub97c\r\n\/\/\uc704\uc640 \uac19\uc774 \ub098\ud0c0\ub0bc \uc218 \uc788\ub2e4.\r\n\/\/\uc704 \ucf54\ub4dc\ub294 str\uc774 null\uc774 \uc544\ub2d0 \uacbd\uc6b0\uc5d0\ub9cc \uc815\uc218\ub85c \ubcc0\uacbd\ud558\uc5ec \ucd9c\ub825\ud55c\ub2e4.\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/with() \ud568\uc218\r\n\/\/fun &lt;T, R&gt; with(receiver: T, block T.() -&gt; R): R\r\nwith(str){\r\n\tprintln(toUpperCase())\r\n}\r\n\/\/with\ud568\uc218\ub294 \uc778\uc218\ub85c \uac1d\uccb4\ub97c \ubc1b\uace0 \ube14\ub85d\uc5d0 \ub9ac\uc2dc\ubc84 \uac1d\uccb4\ub85c \uc804\ub2ec\ud55c\ub2e4.\r\n\/\/\uc218\ud589\ub41c \uacb0\uacfc\ub97c \ubc18\ud658\ud55c\ub2e4.\r\n\/\/\ub9ac\uc2dc\ubc84 \uac1d\uccb4\ub85c \uc804\ub2ec\ub41c \uac1d\uccb4\ub294 this\ub85c \uc811\uadfc \uac00\ub2a5\ud558\ub2e4.\r\n\/\/this\ub294 \uc0dd\ub7b5\uc774 \uac00\ub2a5\ud558\ubbc0\ub85c \uc704\uc640 \uac19\uc774 \uc791\uc131\uc774 \uac00\ub2a5\ud558\uba70, \uc548\uc804\ud55c \ud638\ucd9c\uc774 \ubd88\uac00\ud558\uc5ec str\uc774 null\uac12\uc774\r\n\/\/\uc544\ub2d0 \uacbd\uc6b0\uc5d0\ub9cc \uc0ac\uc6a9\ud574\uc57c \ud55c\ub2e4.\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/apply() \ud568\uc218\r\n\/\/fun &lt;T&gt; T.apply(block: T.() -&gt; Unit): T\r\nval result = car?.apply {\r\n\tcar.setColor(Color.RED)\r\n\tcar.setPrice(1000)\r\n}\r\n\/\/apply\ud568\uc218\ub294 \ube14\ub85d\uc5d0 \uac1d\uccb4 \uc790\uc2e0\uc774 \ub9ac\uc2dc\ubc84 \uac1d\uccb4\ub85c \uc804\ub2ec\ub418\uace0 \uc774 \uac1d\uccb4\uac00 \ubc18\ud658\ub41c\ub2e4.\r\n\/\/\uac1d\uccb4\uc758 \uc0c1\ud0dc\ub97c \ubcc0\ud654\uc2dc\ud0a4\uace0 \uadf8 \uac1d\uccb4\ub97c \ub2e4\uc2dc \ubc18\ud658\ud560 \ub54c \uc8fc\ub85c \uc0ac\uc6a9.\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/run() \ud568\uc218\r\n\/\/fun &lt;R&gt; run(block: () -&gt; R): R\r\nval avg = run {\r\n\tval korean = 100\r\n\tval english = 80\r\n\tval math = 50\r\n\r\n\t(korean + english + math) \/ 3.0\r\n}\r\n\r\n\/\/\uac1d\uccb4\uc5d0\uc11c \ud638\ucd9c\ud558\ub294 \ubc29\ubc95\uc740 \uac1d\uccb4\ub97c \ube14\ub85d\uc758 \ub9ac\uc2dc\ubc84 \uac1d\uccb4\ub85c \uc804\ub2ec\ud558\uace0 \ube14\ub85d\uc758 \uacb0\uacfc\ub97c \ubc18\ud658\ud55c\ub2e4.\r\n\/\/\uc548\uc804\ud55c \ud638\ucd9c\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc5b4\uc11c with \ud568\uc218\ubcf4\ub2e4 \uc720\uc6a9\ud558\ub2e4.\r\n\r\n\/\/fun &lt;T, R&gt; T.run(block: T.() -&gt; R): R\r\nstr?.run {\r\n\tprintln(toUpperCase())\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[349,7,4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/698"}],"collection":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=698"}],"version-history":[{"count":1,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/698\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/698\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}