{"id":497,"date":"2025-06-18T11:20:18","date_gmt":"2025-06-18T03:20:18","guid":{"rendered":"https:\/\/trantor.ink\/?p=497"},"modified":"2025-06-18T11:20:45","modified_gmt":"2025-06-18T03:20:45","slug":"sam","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=497","title":{"rendered":"SAM"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=497\/#%F0%9F%94%8D_%E4%BB%80%E4%B9%88%E6%98%AF_SAM%EF%BC%9F\" >\ud83d\udd0d \u4ec0\u4e48\u662f SAM\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9C%A8_%E2%80%9CRedundant_SAM_constructor%E2%80%9D%E7%9A%84%E6%84%8F%E6%80%9D\" >\u2728 \u201cRedundant SAM constructor\u201d\u7684\u610f\u601d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9C%85_%E7%A4%BA%E4%BE%8B%E5%AF%B9%E6%AF%94\" >\u2705 \u793a\u4f8b\u5bf9\u6bd4<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9D%8C_%E5%86%97%E4%BD%99%E5%86%99%E6%B3%95%EF%BC%88IDE_%E4%BC%9A%E6%8F%90%E7%A4%BA\" >\u274c \u5197\u4f59\u5199\u6cd5\uff08IDE \u4f1a\u63d0\u793a&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/trantor.ink\/?p=497\/#Redundant_SAM_constructor\" >Redundant SAM constructor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/trantor.ink\/?p=497\/#%EF%BC%89%EF%BC%9A\" >\uff09\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9C%85_%E6%8E%A8%E8%8D%90%E5%86%99%E6%B3%95%EF%BC%9A\" >\u2705 \u63a8\u8350\u5199\u6cd5\uff1a<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/trantor.ink\/?p=497\/#%F0%9F%9A%AB_IDE_%E5%BF%AB%E6%8D%B7%E4%BF%AE%E5%A4%8D\" >\ud83d\udeab IDE \u5feb\u6377\u4fee\u590d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9A%A0%EF%B8%8F_%E6%B3%A8%E6%84%8F%E5%9C%BA%E6%99%AF\" >\u26a0\ufe0f \u6ce8\u610f\u573a\u666f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/trantor.ink\/?p=497\/#%E2%9C%85_%E6%80%BB%E7%BB%93\" >\u2705 \u603b\u7ed3<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">\u201c<strong>Redundant SAM constructor<\/strong>\u201d \u662f Kotlin \u7f16\u8bd1\u5668\u6216 IDE\uff08\u5982 Android Studio\uff09\u5728 <strong>lambda \u8868\u8fbe\u5f0f\u8f6c\u63a5\u53e3\u5b9e\u4f8b\uff08SAM\uff09<\/strong> \u65f6\u7ed9\u51fa\u7684 <strong>\u5197\u4f59\u4ee3\u7801\u63d0\u793a<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%94%8D_%E4%BB%80%E4%B9%88%E6%98%AF_SAM%EF%BC%9F\"><\/span><strong>\ud83d\udd0d \u4ec0\u4e48\u662f SAM\uff1f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>SAM<\/strong>\uff08Single Abstract Method\uff09\u662f\u6307\u53ea\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\uff0c\u6bd4\u5982 Java \u7684 Runnable\u3001OnClickListener \u7b49\u3002Kotlin \u652f\u6301\u76f4\u63a5\u7528 lambda \u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u8fd9\u4e9b\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u79f0\u4e3a <strong>SAM \u8f6c\u6362<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%A8_%E2%80%9CRedundant_SAM_constructor%E2%80%9D%E7%9A%84%E6%84%8F%E6%80%9D\"><\/span><strong>\u2728 \u201cRedundant SAM constructor\u201d\u7684\u610f\u601d<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u7528\u4e86\u4e00\u4e2a <strong>\u663e\u5f0f\u7684 SAM \u6784\u9020\u5668\u51fd\u6570<\/strong>\uff08\u6bd4\u5982 Runnable { &#8230; } \u6216 object : Runnable { &#8230; }\uff09\uff0c\u4f46 Kotlin \u53ef\u4ee5 <strong>\u81ea\u52a8\u5c06 lambda \u8f6c\u6362\u6210\u63a5\u53e3\u5b9e\u4f8b<\/strong>\uff0c\u6240\u4ee5\u63d0\u793a\u4f60\u8fd9\u662f\u300c\u5197\u4f59\u300d\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E7%A4%BA%E4%BE%8B%E5%AF%B9%E6%AF%94\"><\/span><strong>\u2705 \u793a\u4f8b\u5bf9\u6bd4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9D%8C_%E5%86%97%E4%BD%99%E5%86%99%E6%B3%95%EF%BC%88IDE_%E4%BC%9A%E6%8F%90%E7%A4%BA\"><\/span><strong>\u274c \u5197\u4f59\u5199\u6cd5\uff08IDE \u4f1a\u63d0\u793a&nbsp;<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Redundant_SAM_constructor\"><\/span><strong>Redundant SAM constructor<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%EF%BC%89%EF%BC%9A\"><\/span><strong>\uff09\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\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 r = Runnable { println(\"Run!\") } \/\/ Kotlin \u80fd\u81ea\u52a8\u8f6c\uff0c\u663e\u5f0f\u5199 Runnable \u662f\u591a\u4f59\u7684<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E6%8E%A8%E8%8D%90%E5%86%99%E6%B3%95%EF%BC%9A\"><\/span><strong>\u2705 \u63a8\u8350\u5199\u6cd5\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\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 r = { println(\"Run!\") } \/\/ Kotlin \u4f1a\u81ea\u52a8\u63a8\u65ad\u6210 Runnable\uff08\u5982\u679c\u4e0a\u4e0b\u6587\u9700\u8981\uff09<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u5728\u51fd\u6570\u8c03\u7528\u4e2d\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=\"\">fun doSomething(runnable: Runnable) {}\n\ndoSomething(Runnable { println(\"Hello\") }) \/\/ \u274c \u5197\u4f59\ndoSomething { println(\"Hello\") }           \/\/ \u2705 \u63a8\u8350<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%9A%AB_IDE_%E5%BF%AB%E6%8D%B7%E4%BF%AE%E5%A4%8D\"><\/span><strong>\ud83d\udeab IDE \u5feb\u6377\u4fee\u590d<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Android Studio \u6216 IntelliJ \u4e2d\uff0c\u5149\u6807\u79fb\u5230\u63d0\u793a\u4e0a\uff0c\u70b9\u51fb \u201cReplace with lambda\u201d \u5373\u53ef\u81ea\u52a8\u7b80\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9A%A0%EF%B8%8F_%E6%B3%A8%E6%84%8F%E5%9C%BA%E6%99%AF\"><\/span><strong>\u26a0\ufe0f \u6ce8\u610f\u573a\u666f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u662f\u6240\u6709\u65f6\u5019\u90fd\u80fd\u7701\u7565\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=\"\">val runnable = Runnable { println(\"A\") } \/\/ \u5408\u7406\uff0c\u5982\u679c\u4f60\u660e\u786e\u60f3\u521b\u5efa Runnable \u5bf9\u8c61<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u5982\u679c\u4f60\u662f\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4f20 lambda \u7ed9\u4e00\u4e2a\u53ea\u63a5\u53d7 SAM \u7684\u53c2\u6570\uff0c<strong>\u5c31\u53ef\u4ee5\u7b80\u5316<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E6%80%BB%E7%BB%93\"><\/span><strong>\u2705 \u603b\u7ed3<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u7528\u6cd5<\/strong><\/th><th><strong>\u662f\u5426\u5197\u4f59<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Runnable { &#8230; }<\/td><td>\u2705 \u5197\u4f59\uff0c\u53ef\u7528 lambda \u66ff\u4ee3<\/td><\/tr><tr><td>object : Runnable { &#8230; }<\/td><td>\u274c \u5408\u7406\uff08\u7279\u522b\u662f\u591a\u65b9\u6cd5\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u201cRedundant SAM constructor\u201d \u662f Kotlin \u7f16\u8bd1\u5668\u6216 IDE\uff08\u5982 Android Studio\uff09\u5728 lambda \u8868\u8fbe\u5f0f\u8f6c\u63a5\u53e3\u5b9e\u4f8b\uff08SAM\uff09 \u65f6\u7ed9\u51fa\u7684 \u5197\u4f59\u4ee3\u7801\u63d0\u793a\u3002 \ud83d\udd0d \u4ec0\u4e48\u662f SAM\uff1f SAM\uff08Single Abstract Method\uff09\u662f\u6307\u53ea\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\uff0c\u6bd4\u5982 Java \u7684 Runnable\u3001OnClickListener \u7b49\u3002Kotlin \u652f\u6301\u76f4\u63a5\u7528 lambda \u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u8fd9\u4e9b\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u79f0\u4e3a SAM \u8f6c\u6362\u3002 \u2728 \u201cRedundant SAM constructor\u201d\u7684\u610f\u601d \u4f60\u7528\u4e86\u4e00\u4e2a \u663e\u5f0f\u7684 SAM \u6784\u9020\u5668\u51fd\u6570\uff08\u6bd4\u5982 Runnable { &#8230; } \u6216 object : Runnable { &#8230; }\uff09\uff0c\u4f46 Kotlin \u53ef\u4ee5 \u81ea\u52a8\u5c06 lambda \u8f6c\u6362\u6210\u63a5\u53e3\u5b9e\u4f8b\uff0c\u6240\u4ee5\u63d0\u793a\u4f60\u8fd9\u662f\u300c\u5197\u4f59\u300d\u7684\u3002 \u2705 \u793a\u4f8b\u5bf9\u6bd4 \u274c \u5197\u4f59\u5199\u6cd5\uff08IDE \u4f1a\u63d0\u793a&nbsp; Redundant SAM constructor \uff09\uff1a \u2705 \u63a8\u8350\u5199\u6cd5\uff1a \u6216\u5728\u51fd\u6570\u8c03\u7528\u4e2d\uff1a \ud83d\udeab IDE \u5feb\u6377\u4fee\u590d \u5728 Android Studio \u6216 IntelliJ \u4e2d\uff0c\u5149\u6807\u79fb\u5230\u63d0\u793a\u4e0a\uff0c\u70b9\u51fb \u201cReplace with lambda\u201d \u5373\u53ef\u81ea\u52a8\u7b80\u5316\u3002 \u26a0\ufe0f \u6ce8\u610f\u573a\u666f \u4e0d\u662f\u6240\u6709\u65f6\u5019\u90fd\u80fd\u7701\u7565\uff1a \u4f46\u5982\u679c\u4f60\u662f\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4f20 lambda \u7ed9\u4e00\u4e2a\u53ea\u63a5\u53d7 SAM \u7684\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u7b80\u5316\u3002 \u2705 \u603b\u7ed3 \u7528\u6cd5 \u662f\u5426\u5197\u4f59 Runnable { &#8230; } \u2705 \u5197\u4f59\uff0c\u53ef\u7528 lambda \u66ff\u4ee3 object : Runnable { &#8230; } \u274c \u5408\u7406\uff08\u7279\u522b\u662f\u591a\u65b9\u6cd5\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\uff09<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[61],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-appdev","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=497"}],"version-history":[{"count":2,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/497\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}