{"id":1001,"date":"2017-08-11T22:40:44","date_gmt":"2017-08-11T14:40:44","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=1001"},"modified":"2021-07-21T14:39:25","modified_gmt":"2021-07-21T06:39:25","slug":"androidkotlin%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%984-%e7%9a%84lambda%e8%a1%a8%e9%81%94%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=1001","title":{"rendered":"[Android]Kotlin\u5b78\u7fd2\u7b46\u8a18#4-\u7684lambda\u8868\u9054\u5f0f"},"content":{"rendered":"\n<p>\u6700\u8fd1\u6b63\u5728\u5b78\u7fd2\u4f7f\u7528Kotlin\u7684lambda\u8868\u9054\u5f0f\u3002<\/p>\n\n\n\n<p>\u5c0d\u65bc\u4ee5\u524d\u7fd2\u6163\u5bebJava\u7684\u7684\u6211\u4f86\u8aaa\uff0c\u9084\u883b\u964c\u751f\u7684&#8230;<\/p>\n\n\n\n<p><del datetime=\"2021-07-21T02:32:48+00:00\">(\u96d6\u7136Java 8\u4e5f\u6709\u5c0e\u5165lambda\u8868\u9054\u5f0f\uff0c\u4f46\u59cb\u7d42\u6c92\u6709\u7528\u904e)<\/del><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>lambda\u8a31\u591a\u4eba\u628a\u5b83\u7ffb\u6210<a rel=\"noreferrer noopener\" href=\"https:\/\/zh.wikipedia.org\/wiki\/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0\" data-type=\"URL\" data-id=\"https:\/\/zh.wikipedia.org\/wiki\/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0\" target=\"_blank\">\u533f\u540d\u51fd\u5f0f<\/a>\uff0c\u610f\u601d\u5c31\u662f\u4e0d\u9700\u8981\u5b9a\u7fa9\u51fd\u5f0f\u540d\u7a31\u7684\u526f\u7a0b\u5f0f\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u4e3b\u8981\u7528\u65bc\u59d4\u6d3e\u3001\u533f\u540d\u51fd\u5f0f\uff0clambda\u8868\u9054\u5f0f\u53ef\u4ee5\u88ab\u7576\u4f5c\u5f15\u6578\u50b3\u905e\u6216\u662f\u8fd4\u56de\u503c\uff0c\u4ee5\u4e0b\u662f\u76ee\u524d\u5b78\u7fd2\u7684\u7c97\u7565\u7d00\u9304\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">lambda\u8868\u9054\u5f0f\u7684\u7528\u6cd5<\/h3>\n\n\n\n<p>\u9019\u88e1\u4f7f\u7528<a rel=\"noreferrer noopener\" href=\"https:\/\/play.kotlinlang.org\/byExample\/04_functional\/02_Lambdas\" data-type=\"URL\" data-id=\"https:\/\/play.kotlinlang.org\/byExample\/04_functional\/02_Lambdas\" target=\"_blank\">\u5b98\u65b9\u7bc4\u4f8b<\/a>\u4f86demo\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">\/\/ All examples create a function object that performs upper-casing.\n\/\/ So it's a function from String to String\n\nval upperCase1: (String) -> String = { str: String -> str.uppercase() } \/\/ 1\n\nval upperCase2: (String) -> String = { str -> str.uppercase() }         \/\/ 2\n\nval upperCase3 = { str: String -> str.uppercase() }                     \/\/ 3\n\n\/\/ val upperCase4 = { str -> str.uppercase() }                          \/\/ 4\n\nval upperCase5: (String) -> String = { it.uppercase() }                 \/\/ 5\n\nval upperCase6: (String) -> String = String::uppercase                  \/\/ 6\n\nprintln(upperCase1(\"hello\"))\nprintln(upperCase2(\"hello\"))\nprintln(upperCase3(\"hello\"))\nprintln(upperCase5(\"hello\"))\nprintln(upperCase6(\"hello\"))<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u9762\u7a0b\u5f0f\u78bc\u76844\u30016\u30018\u300112\u300114\u6240\u793a\uff0c=\u5de6\u908a\u70ba\u5ba3\u544a\u7684\u578b\u5225\uff0c=\u53f3\u908a\u5247\u662flambda\u8868\u9054\u5f0f\u8981\u57f7\u884c\u7684\u5167\u5bb9\u3002<\/p>\n\n\n\n<p>lambda\u8868\u9054\u5f0f\u7684\u95dc\u9375\u5b57\u662f\u7bad\u982d(->)\uff0c\u5ba3\u544a\u6642\u7bad\u982d\u524d\u65b9\u8981\u653e\u4e0a\u50b3\u5165\u7684\u53c3\u6578\u53ca\u578b\u614b\uff0c\u5f8c\u65b9\u5247\u662f\u8981\u56de\u50b3\u7684\u578b\u5225\u3002<\/p>\n\n\n\n<p>lambda\u7684\u4e3b\u9ad4\u4ee5 <code>{ }<\/code> \u5305\u8457\uff0c\u7bad\u982d\u524d\u65b9\u8868\u793a\u7684\u662f\u50b3\u5165\u7684\u53c3\u6578\uff0c\u5f8c\u65b9\u5247\u662f\u8981\u8655\u7406\u7684\u5167\u5bb9\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>lambda\u8868\u9054\u5f0f\u4e5f\u53ef\u4ee5\u50b3\u5165\u591a\u500b\u53c3\u6578\uff0c\u53c3\u6578\u9593\u9694\u7528 , \u5340\u9694\uff0c\u5982\u4e0b\u65b9\u7a0b\u5f0f\u78bc\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val sumString1 = { str1: String, str2: String -> str1 + str2}\n    \nval sumString2: (String, String) -> String = {str1, str2 -> str1 + str2}\n    \nprintln(sumString1(\"Hello\", \"World\"))\nprintln(sumString2(\"Hello\", \"World\"))<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>lambda\u9084\u53ef\u4ee5\u4f5c\u70ba\u5f15\u6578\u65b9\u5f0f\u50b3\u905e\uff0c\u5982\u4e0b\u65b9\u7a0b\u5f0f\u78bc\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">fun main() {\n    \/\/ Lambda \u8868\u9054\u5f0f\uff0c\u96a8\u6a5f\u751f\u62100~100\u7684\u4efb\u610f\u6578\u4e26\u56de\u50b3\n    var func = {\n    \t(0..100).random()\n    }\n    \n    process(\"Operation 1 Finish\", func)\n    \n    \/\/ Lambda \u8868\u9054\u5f0f\u7c21\u5316\n    process(\"Operation 2 Finish\") { 1 }\n}\n\nfun process(str: String, func: () -> Int) {\n    println(\"In process\")\n    println(\"value: ${func.invoke()}\")\n    println(str)\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6700\u5f8c\uff0cJava \u7684 interface \u985e\u5225\uff0c\u5982\u679c\u53ea\u6709\u4e00\u500b\u65b9\u6cd5\u3002\u5728 Kotlin \u4e0a\u4f7f\u7528\u6642\uff0c\u4e5f\u53ef\u4ee5\u900f\u904eLambda\u8868\u9054\u5f0f\u4f86\u547c\u53eb\u3002\u9019\u500b\u884c\u70ba\u53eb\u505a<a rel=\"noreferrer noopener\" href=\"https:\/\/kotlinlang.org\/docs\/java-interop.html#sam-conversions\" data-type=\"URL\" data-id=\"https:\/\/kotlinlang.org\/docs\/java-interop.html#sam-conversions\" target=\"_blank\">SAM conversions<\/a>\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">button.setOnClickListener { it ->\n    it.text = \"\u6211\u88ab\u6309\u4e0b\u4e86\"\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u5f8c<\/h3>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u6211\u76ee\u524d\u5b78\u7fd2\u5230\u7684 lambda \u8868\u9054\u5f0f\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u4ee5\u5f80\u5728\u5bebJava\u7684\u7a0b\u5f0f\u6642\uff0c\u5e38\u5e38\u6703\u56e0\u70ba\u8981\u8de8\u985e\u5225\u50b3\u905e\u8cc7\u6599\u800c\u5be6\u4f5c\u4e00\u7cfb\u5217\u7684interface\u3002<\/p>\n\n\n\n<p>\u7528\u4e86Kotlin\u4e0a\u7684Lambda\u8868\u9054\u5f0f\u4e4b\u5f8c\uff0c\u8de8\u985e\u5225\u50b3\u905e\u8cc7\u6599\u8b8a\u5f97\u66f4\u7c21\u55ae\u4e86\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u4e5f\u4e0d\u7528\u5be6\u4f5c\u4e00\u7cfb\u5217\u7684\u4ecb\u9762\u985e\u5225\uff0c\u53ea\u9700\u5ba3\u544apublic\u7684lambda\u8868\u9054\u5f0f\u8b8a\u6578\u5373\u53ef<\/p>\n\n\n\n<p>\u5982\u6b64\u4e00\u4f86\u4e0d\u4f46\u5927\u5e45\u6e1b\u5c11\u4e86\u7a0b\u5f0f\u78bc\u91cf\uff0c\u800c\u4e14\u7a0b\u5f0f\u78bc\u4e5f\u56e0\u6b64\u7c21\u6f54\u8a31\u591a\u3002<\/p>\n\n\n\n<p>\u6700\u5f8c\uff0c\u5b78\u5230\u65b0\u6280\u8853\u662f\u4e00\u4ef6\u958b\u5fc3\u7684\u4e8b\u60c5\u3002\u4e4b\u5f8c\u82e5\u6709\u66f4\u591a\u61c9\u7528\u7684\u7d93\u9a57\uff0c\u5728\u518d\u88dc\u5145\u4e0a\u4f86\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6b63\u5728\u5b78\u7fd2\u4f7f\u7528Kotlin\u7684lambda\u8868\u9054\u5f0f\u3002 \u5c0d\u65bc\u4ee5\u524d\u7fd2\u6163\u5bebJava\u7684\u7684\u6211\u4f86\u8aaa\uff0c\u9084\u883b\u964c\u751f\u7684&#8230; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,19],"class_list":["post-1001","post","type-post","status-publish","format-standard","hentry","category-2","tag-android-app","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1001","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=1001"}],"version-history":[{"count":4,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1001\/revisions"}],"predecessor-version":[{"id":1005,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1001\/revisions\/1005"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}