{"id":2229,"date":"2023-05-06T22:02:03","date_gmt":"2023-05-06T14:02:03","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2229"},"modified":"2023-05-07T09:53:22","modified_gmt":"2023-05-07T01:53:22","slug":"groovy-code-example-closure","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/06\/groovy-code-example-closure\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; \u95ed\u5305closure"},"content":{"rendered":"<h2>groovy\u95ed\u5305closure<\/h2>\n<p>\u95ed\u5305\u5c31\u662f\u5e26\u6709\u53c2\u6570\u3001\u8fd4\u56de\u503c\u5e76\u4e14\u80fd\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf\u7684\u533f\u540d\u4ee3\u7801\u5757 \u3002Groovy\u4e2d\u7684\u95ed\u5305\u80fd\u591f\u8bbf\u95ee\u95ed\u5305\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u5728\u95ed\u5305\u4f20\u7edf\u5b9a\u4e49\u7684\u57fa\u7840\u4e0a\uff0cGroovy\u6269\u5c55\u4e86\u4e00\u4e9b\u7279\u6027\u3002\u901a\u8fc7\u8fd9\u4e9b\u7279\u6027\u7684\u652f\u6301\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u95ed\u5305\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u5728\u51fd\u6570\u5185\u8c03\u7528\u533f\u540d\u7684\u4ee3\u7801\u5757\uff0c\u4ece\u800c\u5199\u51fa\u66f4\u52a0\u52a8\u6001\u7684\u4ee3\u7801\uff0c\u7b49\u7b49\u3002\u63a5\u4e0b\u6765\u770b\u770bgroovy\u95ed\u5305\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u95ed\u5305\ndef myClosure = {\n    println &quot;Hello World!&quot;\n}\n\n\/\/\u6267\u884c\u95ed\u5305\u65b9\u5f0f1\nmyClosure.call() \/\/ Hello World!\n\n\/\/\u6267\u884c\u95ed\u5305\u65b9\u5f0f2\nmyClosure() \/\/ Hello World!<\/code><\/pre>\n<h2>\u95ed\u5305\u7684\u53c2\u6570<\/h2>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u53c2\u6570\u7ed9\u95ed\u5305\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u4f60\u53ef\u4ee5\u7f16\u5199\u5177\u6709\u5e26\u6709\u53c2\u6570\u7684\u95ed\u5305\uff0c\u7136\u540e\u8c03\u7528\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<pre><code class=\"language-java\">\/\/it\u4e3a\u5185\u7f6e\u53c2\u6570\ndef squareWithImplicitParameter = { it * it }\n\/\/\u8c03\u7528\u95ed\u5305\nprintln squareWithImplicitParameter(4) \/\/ 16\n\n\/\/\u5b9a\u4e49\u4e86a,b\u4e24\u4e2a\u53c2\u6570\uff0c\u90fd\u58f0\u660e\u4e3aint\ndef sumWithExplicitTypes = { int a, int b -&gt; return a + b }\nprintln sumWithExplicitTypes(11, 8) \/\/ 19\n\n\/\/\u5b9a\u4e49\u4e86a,b\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u58f0\u660e\u4e86a\u7684\u7c7b\u578b\u4e3aint\ndef sumWithOneExplicitOneOptionalTypes = { int a, b -&gt; return a + b }\nprintln sumWithOneExplicitOneOptionalTypes(20, 13) \/\/ 33\n\n\/\/\u5b9a\u4e49\u4e86a,b\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u8bbe\u5b9ab\u7684\u9ed8\u8ba4\u503c\u4e3a5\uff0c\u5982\u679c\u8c03\u7528\u95ed\u5305\u65f6\u6ca1\u6709\u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u4e3a5\ndef sumWithDefaultParameterValue = { a, b = 5 -&gt; return a + b }\nprintln sumWithDefaultParameterValue(4)  \/\/ 9<\/code><\/pre>\n<h2>\u95ed\u5305closure\u7684\u53d8\u957f\u53c2\u6570varargs<\/h2>\n<p>\u53ef\u4ee5\u4e3a\u95ed\u5305\u5b9a\u4e49\u4e00\u4e2a\u53d8\u957f\u7684\u53c2\u6570\uff0c\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u53d8\u957f\u53c2\u6570String... names\uff0c\u7136\u540e\u5c06\u53c2\u6570\u7528\u9017\u53f7\u8fde\u63a5\ndef combine = { String... names -&gt;\n    names.join(&#039;,&#039;)\n}\n\nprintln combine(&#039;John&#039;, &#039;Doe&#039;, &#039;Betty&#039;) \/\/ John,Doe,Betty<\/code><\/pre>\n<h2>\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570<\/h2>\n<p>groovy\u652f\u6301\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u6216\u5176\u4ed6\u95ed\u5305<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e00\u4e2a\u95ed\u5305,\u5176\u4e2d\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u53e6\u5916\u4e00\u4e2a\u95ed\u5305\ndef funcClosure = { x, func -&gt;\n    \/\/\u6267\u884c\u4f20\u5165\u7684\u95ed\u5305\n    func(x)\n}\n\n\/\/{it.size()} \u662f\u4e00\u4e2a\u95ed\u5305\nprintln funcClosure([1, 2, 3], { it.size() }) \/\/ 3\n\n\/\/\u7b49\u4ef7\u4e8e\ndef anotherClosure = {\n    it.size()\n}\n\nprintln funcClosure([1, 2, 3], anotherClosure) \/\/ 3<\/code><\/pre>\n<h2>\u95ed\u5305\u7684\u7ec4\u5408<\/h2>\n<p>groovy\u95ed\u5305\u652f\u6301\u7ec4\u5408\uff0c\u901a\u8fc7\u95ed\u5305\u7684\u7ec4\u5408\u5b9e\u73b0\u91cd\u7528\u5e76\u5efa\u7acb\u66f4\u52a0\u590d\u6742\u7684\u95ed\u5305<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u52a0\u6cd5\u95ed\u5305\ndef sum = { a, b -&gt; return a + b }\n\/\/\u5b9a\u4e49\u4e00\u4e2a\u8ba1\u7b97\u9762\u79ef\u7684\u95ed\u5305\ndef square = { it * it }\n\n\/\/\u7ec4\u5408\u95ed\u5305\u5f62\u6210\u65b0\u7684\u95ed\u5305\uff0c\u5c06sum\u95ed\u5305\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9square\u95ed\u5305\ndef squareOfSum = square &lt;&lt; sum\n\/\/\u76f8\u5f53\u4e8e{ { a, b -&gt; return a + b } * { a, b -&gt; return a + b }}\n\nprintln squareOfSum(2, 3) \/\/ 25<\/code><\/pre>\n<h2>groovy\u4e2d\u5982\u4f55\u4ece\u95ed\u5305.each{Closure} \u4e2d\u65ad\u6267\u884c\u5e76\u9000\u51fa\uff1f<\/h2>\n<p>groovy\u4e2d\u7684\u96c6\u5408\u53ef\u4ee5\u6267\u884c.each{Closure}\u95ed\u5305\uff0c\u8fd9\u4e2a\u95ed\u5305\u672c\u8eab\u662f\u4e0d\u652f\u6301\u4e2d\u9014\u9000\u51fa\uff08\u9664\u975e\u629b\u51fa\u5f02\u5e38\uff0c\u4e00\u822c\u4e0d\u5efa\u8bae\u8fd9\u6837\u8bbe\u8ba1\uff09\u3002\u4f46\u662f\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u66ff\u4ee3\u7684\u95ed\u5305\u6765\u8fbe\u5230\u76f8\u540c\u7684\u76ee\u6807\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>find\u95ed\u5305<\/p>\n<pre><code class=\"language-java\">def a = [1, 2, 3, 4, 5, 6, 7]\n\na.find {\n    if (it &gt; 5) return true \/\/ \u9000\u51fa\u904d\u5386\n    println it  \/\/ \u6267\u884c\u904d\u5386\u64cd\u4f5c\n    return false \/\/ \u7ee7\u7eed\u904d\u5386\n}<\/code><\/pre>\n<p>\u6216\u8005any\u95ed\u5305<\/p>\n<pre><code class=\"language-java\">def list = [1, 2, 3, 4, 5]\nlist.any { it -&gt;\n    if (it == 2)\n        return \/\/ \u7ee7\u7eed\u6267\u884c\n\n    println it\n\n    if (it == 3)\n        return true \/\/\u9000\u51fa\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>groovy\u95ed\u5305closure \u95ed\u5305\u5c31\u662f\u5e26\u6709\u53c2\u6570\u3001\u8fd4\u56de\u503c\u5e76\u4e14\u80fd\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf\u7684\u533f\u540d\u4ee3\u7801\u5757 \u3002Groovy\u4e2d\u7684\u95ed\u5305 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2229","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2229"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2229\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}