{"id":1259,"date":"2022-07-29T22:52:01","date_gmt":"2022-07-29T14:52:01","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1259"},"modified":"2024-03-21T17:04:01","modified_gmt":"2024-03-21T09:04:01","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a03","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/07\/29\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a03\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60(3)"},"content":{"rendered":"\n<!--more-->\n\n\n\n<p class=\"has-large-font-size\">\u6570\u636e\u7c7b\u4e0e\u5355\u4f8b\u7c7b<\/p>\n\n\n\n<p>\u5728Java\u4e2d\uff0c\u6570\u636e\u7c7b\u5360\u636e\u7740\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5176\u901a\u5e38\u9700\u8981\u91cd\u5199\u5305\u62ec<code>equals()<\/code>,<code>hashCode()<\/code>,<code>toString()<\/code>\u51e0\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528Java\u6765\u5199\u9700\u8981\u7ee7\u627f\u5404\u4e2a\u65b9\u6cd5\u5e76\u9010\u4e2a\u586b\u5145\uff0c\u4f46Kotlin\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u6570\u636e\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">data class Cellphone(val brand: String, val price: Double)<\/code><\/pre>\n\n\n\n<p>\u5f53\u7528 data \u58f0\u660e\u65f6\u5c31\u8868\u660e\u8fd9\u4e2a\u7c7b\u4e3a\u6570\u636e\u7c7b\u3002Kotlin\u4f1a\u6839\u636e\u4e3b\u6784\u9020\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u81ea\u52a8\u751f\u6210\u5305\u62ec<code>equals()<\/code>,<code>hashCode()<\/code>,<code>toString()<\/code>\u7b49\u56fa\u5b9a\u65e0\u5b9e\u9645\u610f\u4e49\u7684\u65b9\u6cd5\u81ea\u52a8\u751f\u6210\u3002\u5e76\u5f53\u7c7b\u6ca1\u6709\u4efb\u4f55\u4ee3\u7801\u65f6\uff0c\u5c3e\u90e8\u5927\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5355\u4f8b\u7c7b\u662fKotlin\u7279\u6709\u7684\u529f\u80fd\uff0c\u5176\u5b9e\u73b0\u7684\u662f\u6700\u5e38\u7528\u3001\u6700\u57fa\u7840\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u4e00\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u907f\u514d\u521b\u5efa\u91cd\u590d\u7684\u5bf9\u8c61\u3002\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u67d0\u4e2a\u7c7b\u5728\u5168\u5c40\u6700\u591a\u53ea\u80fd\u62e5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u3002Java\u4e2d\u6700\u5e38\u89c1\u7684\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">public class Singleton {\n     private static Singleton instance;\n     private Singleton() {}\n     public synchronized static Singleton getInstance() {\n        if (instance == null) {\n           instance = new Singleton();\n        }\n        return instance;\n     }\n     public void singletonTest() {\n         System.out.println(\"singletonTest is called.\");\n     }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u901a\u8fc7\u5c06\u6784\u9020\u51fd\u6570\u79c1\u6709\u5316\uff0c\u518d\u8bbe\u7f6e\u9759\u6001\u65b9\u6cd5\u83b7\u53d6\u5bf9\u8c61\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u3002<\/p>\n\n\n\n<p>\u4f46\u5728Kotlin\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u521b\u5efa\u5355\u4f8b\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">object Singleton {\n    fun singletonTest() {\n       println(\"singletonTest is called.\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u521b\u5efa\u4e86\u5355\u4f8b\u7c7b\uff0c\u800c\u8c03\u7528\u4e5f\u548cJava\u4e2d\u7684\u9759\u6001\u65b9\u6cd5\u8c03\u7528\u5dee\u4e0d\u591a\u4f7f\u7528 <code>Singleton.singletonTest()<\/code> \u8c03\u7528\u5373\u53ef\u3002<\/p>\n\n\n\n<div style=\"height:38px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Lambda\u7f16\u7a0b<\/p>\n\n\n\n<p>Kotlin\u539f\u751f\u652f\u6301Lambda\u7f16\u7a0b<\/p>\n\n\n\n<p>\u5982\u6211\u4eec\u521b\u5efa\u4e00\u4e2aArrayList\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val list = listOf(\"Apple\", \"Banana\", \"Orange\", \"Pear\", \"Grape\")<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec <code>listof()<\/code> \u8fd9\u4e2aKotlin\u65b9\u6cd5\u6765\u5feb\u901f\u5b9e\u73b0\u96c6\u5408\u7684\u521b\u5efa\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u521b\u5efa\u7684\u662f\u4e0d\u53ef\u53d8\u96c6\u5408\uff0c\u4f7f\u7528 <code>mutableListOf()<\/code> \u53ef\u4ee5\u521b\u5efa\u53ef\u53d8\u96c6\u5408\u3002\u540c\u7406\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>setOf<\/code> \u548c <code>mutableSetOf()<\/code>\u521b\u5efaSet\u3002<code>mapOf()<\/code> \u548c <code>mutableMapOf()<\/code>\u521b\u5efamap\uff0c\u4e0d\u8fc7\u5728Kotlin\u4e2d\uff0cmap\u521b\u5efa\u4f7f\u7528 to \u8fde\u63a5 K-V\uff1a<code>mapOf(\"Apple\" to 1, \"Banana\" to 2, \"Orange\" to 3, \"Pear\" to 4, \"Grape\" to 5)<\/code><\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Lambda\u5bf9\u96c6\u5408\u8fdb\u884c\u904d\u5386\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val list = mutableListOf(\"Apple\", \"Banana\", \"Orange\", \"Pear\", \"Grape\")\n     list.add(\"Watermelon\")\n     for (fruit in list) {\n        println(fruit)\n     }  <\/code><\/pre>\n\n\n\n<p>\u800c\u5bf9map\u7684\u904d\u5386\u9700\u8981\u7528\u62ec\u53f7\u62ec\u4f4fK-V\u7684\u8868\u8fbe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">for ((fruit, number) in map) {\n      println(\"fruit is \" + fruit + \", number is \" + number)\n}<\/code><\/pre>\n\n\n\n<div style=\"height:9px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\">Lambda\u7684\u96c6\u5408\u7684\u51fd\u6570\u5f0fAPI<\/p>\n\n\n\n<p>\u9996\u5148\u5728Kotlin\u4e2d\uff0cLambda\u7684\u8bed\u6cd5\u7ed3\u6784\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">{\u53c2\u6570\u540d1: \u53c2\u6570\u7c7b\u578b, \u53c2\u6570\u540d2: \u53c2\u6570\u7c7b\u578b -> \u51fd\u6570\u4f53}<\/code><\/pre>\n\n\n\n<p>\u4f46\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u662f\u4e0d\u9700\u8981\u4f7f\u7528\u5b8c\u6574\u7684Lambda\u8868\u8fbe\u5f0f\u7684\uff0c\u6709\u5f88\u591a\u79cd\u7b80\u5316\u7684\u5199\u6cd5\u3002<\/p>\n\n\n\n<p>\u5982\u6211\u4eec\u4f7f\u7528Lambda\u5bfb\u627e\u6700\u957f\u5355\u8bcd\u6c34\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val lambda = { fruit: String -> fruit.length }\nval maxLengthFruit = list.maxBy(lambda)<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4e0a\u9762\u5b9a\u4e49\u4e86Lambda\u8868\u8fbe\u5f0f\uff0c\u4e0b\u9762\u4f7f\u7528list\u7684maxBy()\u65b9\u6cd5\u5176\u53ea\u662f\u66f4\u5177\u4f20\u5165\u7684\u904d\u5386\u96c6\u5408\u6765\u5bfb\u627e\u6700\u5927\u503c\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u8fdb\u884c\u7b80\u5316\u7684\u7b2c\u4e00\u6b65\u5c31\u662f\u4e0d\u9700\u8981\u4e13\u95e8\u5b9a\u4e49\u4e00\u4e2alambda\u53d8\u91cf\uff0c\u800c\u662f\u76f4\u63a5\u5c06lambda\u4f20\u5165\u65b9\u6cd5\u5185\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val maxLengthFruit = list.maxBy({ fruit: String -> fruit.length })<\/code><\/pre>\n\n\n\n<p>\u7136\u540eKotlin\u89c4\u5b9a\uff0c\u5f53Lambda\u53c2\u6570\u662f\u51fd\u6570\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u79fb\u52a8\u5230\u62ec\u53f7\u5916\u9762\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val maxLengthFruit = list.maxBy() { fruit: String -> fruit.length }<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5982\u679cLambda\u53c2\u6570\u662f\u51fd\u6570\u7684\u552f\u4e00\u4e00\u4e2a\u53c2\u6570\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u62ec\u53f7\u7701\u7565\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val maxLengthFruit = list.maxBy { fruit: String -> fruit.length }<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8eKotlin\u62e5\u6709\u7c7b\u578b\u63a8\u5bfc\u673a\u5236\uff0cLambda\u8868\u8fbe\u5f0f\u4e2d\u7684\u53c2\u6570\u5217\u8868\u5176\u5b9e\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4e0d\u5fc5\u58f0\u660e\u53c2\u6570\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val maxLengthFruit = list.maxBy { fruit -> fruit.length }<\/code><\/pre>\n\n\n\n<p>\u5f53Lambda\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u5217\u8868\u4e2d\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u65f6\uff0c\u4e5f\u4e0d\u5fc5\u58f0\u660e\u53c2\u6570\u540d\uff0c\u800c\u662f\u53ef\u4ee5\u4f7f\u7528 it \u5173\u952e\u5b57\u6765\u4ee3\u66ff\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val maxLengthFruit = list.maxBy { it.length }<\/code><\/pre>\n\n\n\n<div style=\"height:15px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>map\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u51fd\u6570\u5f0fAPI\u4e4b\u4e00\uff0c\u5b83\u7528\u4e8e\u5c06\u96c6\u5408\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u6620\u5c04\u6210\u4e3a\u53e6\u4e00\u4e2a\u503c\uff0c\u5982\u6211\u4eec\u5e0c\u671b\u96c6\u5408\u4e2d\u6240\u6709\u5b57\u6bcd\u53d8\u4e3a\u5927\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val newList = list.map { it.toUpperCase() }<\/code><\/pre>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u51fd\u6570\u5f0fAPI\u4e3a filter\uff0c\u5176\u7528\u4e8e\u8fc7\u6ee4\u96c6\u5408\u4e2d\u7684\u6570\u636e\uff0c\u914d\u5408map\u4f7f\u7528\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val newList = list.filter { it.length &lt;= 5 }  \/\/\u8fc7\u6ee4\u51fa\u4e94\u4e2a\u5b57\u6bcd\u4ee5\u5185\u7684\u6c34\u679c\n                  .map { it.toUpperCase() }   \/\/\u5168\u90e8\u5927\u5199\u8868\u793a<\/code><\/pre>\n\n\n\n<div style=\"height:8px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>any\u548call\u51fd\u6570\u524d\u8005\u7528\u4e8e\u5224\u65ad\u96c6\u5408\u4e2d\u662f\u5426\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u6761\u4ef6\uff0c\u540e\u8005\u5224\u65ad\u662f\u5426\u6240\u6709\u5143\u7d20\u90fd\u6ee1\u8db3\u6761\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val anyResult = list.any { it.length &lt;= 5 }\nval allResult = list.all { it.length &lt;= 5 }<\/code><\/pre>\n\n\n\n<div style=\"height:19px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">\u7a7a\u6307\u9488\u68c0\u67e5<\/p>\n\n\n\n<p>Kotlin\u6709\u7740\u4e25\u683c\u7684\u7a7a\u6307\u9488\u68c0\u67e5\uff0c\u5b83\u5728\u7f16\u8bd1\u65f6\u5c31\u4f1a\u68c0\u67e5\u4ee3\u7801\u662f\u5426\u5b89\u5168\uff08\u4f1a\u4e0d\u4f1a\u51fa\u73b0null\uff09\uff0c\u8fd9\u5f53\u7136\u4f1a\u5bfc\u81f4\u4ee3\u7801\u53d8\u5f97\u6bd4\u8f83\u7537\u978b\uff0c\u4f46\u662fKotlin\u4e5f\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u8f85\u52a9\u5de5\u5177\u3002\u5982:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun doStudy(study: Study) {\n   study.readBooks()\n   study.doHomework()\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u770b\u4f3c\u548cJava\u7248\u7684\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5b89\u5168\u7684\u6ca1\u6709\u7a7a\u6307\u9488\u98ce\u9669\u7684\uff0c\u56e0\u4e3aKotlin\u9ed8\u8ba4\u6240\u6709\u7684\u53c2\u6570\u548c\u53d8\u91cf\u90fd\u662f\u4e0d\u53ef\u4e3a\u7a7a\u7684\uff0c\u6240\u6709\u4f20\u5165\u7684study\u4e5f\u4e0d\u53ef\u80fd\u4e3a\u7a7a\uff0c\u5f53\u4f20\u5165null\u65f6\uff0c\u4f1a\u63d0\u793a\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u4f46\u6709\u65f6\u5019\uff0c\u4e1a\u52a1\u903b\u8f91\u9700\u8981\u6211\u4eec\u4f7f\u7528null\u4f20\u5165\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u5728\u7c7b\u540d\u540e\u52a0\u4e2a \uff1f \u5373\u53ef \u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">fun doStudy(study: Study?)<\/code><\/pre>\n\n\n\n<p>\u4f46\u5982\u679c\u6211\u4eec\u76f4\u63a5\u8fd9\u6837\u66f4\u6539\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u53d1\u73b0\uff0c\u5b83\u7684\u65b9\u6cd5\u8c03\u7528\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5982\u679cstudy\u53ef\u80fd\u4e3a\u7a7a\uff0c\u90a3\u4e48\u5176\u65b9\u6cd5\u4e5f\u53ef\u80fd\u4e3a\u7a7a\uff0c\u4e0d\u7b26\u5408Kotlin\u7684\u89c4\u8303\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u6539\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun doStudy(study: Study?) {\n     if (study != null) {\n       study.readBooks()\n       study.doHomework()\n     }\n}<\/code><\/pre>\n\n\n\n<div style=\"height:14px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">\u5224\u7a7a\u8f85\u52a9\u5de5\u5177<\/p>\n\n\n\n<p>\u9996\u5148\u5b66\u4e60\u6700\u5e38\u7528\u7684?.\u64cd\u4f5c\u7b26\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u7684\u4f5c\u7528\u975e\u5e38\u597d\u7406\u89e3\uff0c\u5c31\u662f\u5f53\u5bf9\u8c61\u4e0d\u4e3a\u7a7a\u65f6\u6b63\u5e38\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\uff0c\u5f53\u5bf9\u8c61\u4e3a\u7a7a\u65f6\u5219\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">\/\/\u7b80\u5316\u524d\nif (a != null) {\n   a.doSomething()\n}\n\/\/\u7b80\u5316\u540e\na?.doSomething()\n\/\/\u540c\u65f6\u4e4b\u524d\u7684\u4ee3\u7801\nfun doStudy(study: Study?) {\n     study?.readBooks()\n     study?.doHomework()\n}<\/code><\/pre>\n\n\n\n<div style=\"height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u63a5\u4e0b\u6765\u53e6\u5916\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684?:\u64cd\u4f5c\u7b26\u3002\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u7684\u5de6\u53f3\u4e24\u8fb9\u90fd\u63a5\u6536\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5de6\u8fb9\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e0d\u4e3a\u7a7a\u5c31\u8fd4\u56de\u5de6\u8fb9\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff0c\u5426\u5219\u5c31\u8fd4\u56de\u53f3\u8fb9\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">\/\/\u7b80\u5316\u524d\nval c = if (a ! = null) {\n   a\n} else {\n   b\n}\n\/\/\u7b80\u5316\u540e\nval c = a ?: b<\/code><\/pre>\n\n\n\n<div style=\"height:8px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u540c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 !! \u6765\u5f3a\u884c\u901a\u8fc7\u7f16\u8bd1\uff0c\u8fd9\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u4f60\u786e\u5b9a\u8fd9\u4e2a\u53d8\u91cf\u4e0d\u4e3a\u7a7a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun printUpperCase() {\n    val upperCase = content!!.toUpperCase()\n    println(upperCase)\n}<\/code><\/pre>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b66\u4e60\u4e00\u4e2a\u8f85\u52a9\u5de5\u5177 let\u3002\u5b83\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u63d0\u4f9b\u4e86\u51fd\u6570\u5f0fAPI\u7684\u7f16\u7a0b\u63a5\u53e3\uff0c\u5e76\u5c06\u539f\u59cb\u8c03\u7528\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230Lambda\u8868\u8fbe\u5f0f\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">obj.let { obj2 ->\n   \/\/ \u7f16\u5199\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u7b80\u5316\u4e4b\u524d\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun doStudy(study: Study?) {\n     study?.let { stu ->\n         stu.readBooks()\n         stu.doHomework()\n     }\n}<\/code><\/pre>\n\n\n\n<p>?.\u64cd\u4f5c\u7b26\u8868\u793a\u5bf9\u8c61\u4e3a\u7a7a\u65f6\u4ec0\u4e48\u90fd\u4e0d\u505a\uff0c\u5bf9\u8c61\u4e0d\u4e3a\u7a7a\u65f6\u5c31\u8c03\u7528let\u51fd\u6570\uff0c\u800clet\u51fd\u6570\u4f1a\u5c06study\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230Lambda\u8868\u8fbe\u5f0f\u4e2d\uff0c\u6b64\u65f6\u7684study\u5bf9\u8c61\u80af\u5b9a\u4e0d\u4e3a\u7a7a\u4e86\uff0c\u6211\u4eec\u5c31\u80fd\u653e\u5fc3\u5730\u8c03\u7528\u5b83\u7684\u4efb\u610f\u65b9\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\u66f4\u5177lambda\u8bed\u6cd5\u7279\u6027\uff0c\u518d\u6b21\u7b80\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\"> study?.let {\n     it.readBooks()\n     it.doHomework()\n}<\/code><\/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":{"spay_email":"","footnotes":""},"categories":[1,16],"tags":[],"class_list":["post-1259","post","type-post","status-publish","format-standard","hentry","category-wcd","category-16"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1259"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1259"}],"version-history":[{"count":1,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1259\/revisions"}],"predecessor-version":[{"id":1260,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1259\/revisions\/1260"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}