{"id":1935,"date":"2024-07-12T19:40:30","date_gmt":"2024-07-12T11:40:30","guid":{"rendered":"https:\/\/blog.xiunian.wang\/?p=1935"},"modified":"2024-08-26T14:19:33","modified_gmt":"2024-08-26T06:19:33","slug":"kotlin-%e8%8c%83%e5%9e%8b%e4%b8%ad%e7%9a%84in-%e5%92%8cout-%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=1935","title":{"rendered":"Kotlin \u8303\u578b\u4e2d\u7684in \u548cout \u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"\n<p>\u6211\u4eec\u770b\u4e00\u4e9b\u6e90\u7801\u53d1\u73b0\uff0c\u4e00\u4e9b\u6e90\u7801\uff0c\u5728\u8303\u578b\u524d\u9762\u6dfb\u52a0\u4e86\u4e00\u4e9b\u5173\u952e\u5b57 in \u6216\u8005 out\uff0c\u8fd9\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\u6bd4\u5982 kotlin\u5185\u7f6e\u7684\u00a0Comparable.kt<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"324\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1024x324.png\" alt=\"\" class=\"wp-image-1936\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1024x324.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-300x95.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-768x243.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u518d\u6bd4\u5982\uff0cCollections.MutableIterable \u63a5\u53e3<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"294\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1-1024x294.png\" alt=\"\" class=\"wp-image-1937\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1-1024x294.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1-300x86.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1-768x220.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-1.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u6709\u4e00\u4e9b\u52a0in\uff0c\u6709\u4e00\u4e9b\u52a0out \u5462\uff1f\u4e86\u89e3\u8fd9\u4e2a\u4e4b\u524d\u6211\u4eec\u6765\u770b\u4e0b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"21b7bca5-0c5e-c24f-5b17-12ba053fdd90\">\u8303\u578b\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p>\u6cdb\u578b\uff08Generics\uff09\u662f\u4e00\u79cd\u5141\u8bb8\u7c7b\u3001\u63a5\u53e3\u548c\u65b9\u6cd5\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\uff08\u7c7b\u578b\u53c2\u6570\uff09\u7684\u7279\u6027\u3002\u6cdb\u578b\u63d0\u4f9b\u4e86\u4e00\u79cd\u7c7b\u578b\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5bf9\u8c61\u96c6\u5408\u548c\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u800c\u65e0\u9700\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u5177\u4f53\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e00\u4e2a\u6817\u5b50\uff1a<\/p>\n\n\n\n<!--more-->\n\n\n<p>class Box<T>(t: T) {<br \/>\n    var value = t<br \/>\n}<\/p>\n\n\n\n<p>\u00a0\u4e3a\u4e86\u521b\u5efa\u8fd9\u4e2aBox\uff0c\u53ef\u4ee5\u53ea\u9700\u63d0\u4f9b\u7c7b\u578b\u53c2\u6570<\/p>\n\n\n<p>val box: Box<Int> = Box<Int>(1)<\/p>\n\n\n\n<p>\u00a0\u5982\u679c\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a8\u65ad\u51fa\u6765\uff0c\u53ef\u4ee5\u7701\u7565\u53c2\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val box = Box(1) <\/code><\/pre>\n\n\n\n<p>\u8fd9\u5f88\u597d\u7406\u89e3 \u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2b0f14a1-e266-f559-96f3-e3eec42ca264\">\u8303\u578b\uff0c<strong>\u662f\u5bf9\u6211\u4eec\u7a0b\u5e8f\u7684\u62bd\u8c61<\/strong><\/h3>\n\n\n\n<p>\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u6211\u4eec\u80fd\u770b\u5230\u5404\u5f0f\u5404\u6837\u7684\u7535\u89c6\u673a\u9065\u63a7\u5668\uff0c\u6bd4\u5982\u5c0f\u7c73\u5c31\u6709 1S\u30012S\u30013S\u30014S \u7535\u89c6\u9065\u63a7\u5668\u3002\u90a3\u4e48\uff0c\u5982\u679c\u6211\u4eec\u5c06\u9065\u63a7\u5668\u7684\u6982\u5ff5\u8fc1\u79fb\u5230\u7a0b\u5e8f\u7684\u4e16\u754c\uff0c\u6211\u4eec\u5c31\u9700\u8981\u5b9a\u4e49\u5404\u79cd\u5404\u6837\u7684\u201c\u9065\u63a7\u5668\u7c7b\u201d\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5c0f\u7c731S\u7535\u89c6\u673a\u9065\u63a7\nclass TvMi1SController {\n    fun turnOn() {}\n    fun turnOff() {}\n}\n\n\/\/ \u5c0f\u7c732S\u7535\u89c6\u673a\u9065\u63a7\nclass TvMi2SController {\n    fun turnOn() {}\n    fun turnOff() {}\n}\n\n\/\/ \u5c0f\u7c733S\u7535\u89c6\u673a\u9065\u63a7\nclass TvMi3SController {\n    fun turnOn() {}\n    fun turnOff() {}\n}\n\n\/\/ \u5c0f\u7c734S\u7535\u89c6\u673a\u9065\u63a7\nclass TvMi4SController {\n    fun turnOn() {}\n    fun turnOff() {}\n}\n\n...\n\u7701\u7565\u51e0\u5343\u79cd\u4e0d\u540c\u7684\u9065\u63a7\u5668<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u6211\u4eec\u4e3a\u6bcf\u4e00\u4e2a\u578b\u53f7\u7684\u7535\u89c6\u673a\u90fd\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u9065\u63a7\u5668\u7c7b\uff0c\u7136\u540e\u5728\u91cc\u9762\u91cd\u590d\u7f16\u5199\u201c\u5f00\u673a\u201d\u201c\u5173\u673a\u201d\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u7684\u5de5\u4f5c\u91cf\u4f1a\u5f88\u5927\uff0c\u800c\u4e14\u6ca1\u6709\u610f\u4e49\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u5176\u5b9e\u9700\u8981\u4e00\u4e2a\u4e07\u80fd\u9065\u63a7\u5668\uff0c\u800c\u501f\u52a9 Kotlin \u7684\u6cdb\u578b\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/          T\u4ee3\u8868\u6cdb\u578b\u7684\u5f62\u53c2\n\/\/               \u2193\nclass Controller&lt;T:TV> {\n    fun turnOn(tv: T) {}\n    fun turnOff(tv: T) {}\n}\n\nfun main() {\n\/\/                                \u6cdb\u578b\u7684\u5b9e\u53c2\n\/\/                                   \u2193\n    val mi1Controller = Controller&lt;XiaoMiTV1>()\n    mi1Controller.turnOn(XiaoMiTV1())\n\n\/\/                                  \u6cdb\u578b\u7684\u5b9e\u53c2\n\/\/                                     \u2193\n    val mi2Controller = Controller&lt;XiaoMiTV2>()\n    mi2Controller.turnOn(XiaoMiTV2()) \n  }<\/pre>\n\n\n\n<p>\u9ed8\u8ba4\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9ed8\u8ba4\u8303\u578b\u7684\u7f3a\u9677<\/h2>\n\n\n\n<p>\u4e0a\u9762\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9065\u63a7\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u5165\u4e0d\u540c\u7c7b\u578b\u7684\u7535\u89c6\u8fdb\u884c\u9065\u63a7\uff0c\u800c\u4e14\u4e24\u4e2a\u9065\u63a7\u5668\u4e92\u4e0d\u5f71\u54cd\uff0c\u56e0\u4e3a\uff0c\u9065\u63a7\u5668\u4e0d\u80fd\u63a7\u5236\u9664\u4e86\u81ea\u8eab\u578b\u53f7\u4e4b\u5916\u7684\u5176\u4ed6\u7535\u89c6\uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"108\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-2.png\" alt=\"\" class=\"wp-image-1943\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-2.png 712w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-2-300x46.png 300w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u7b26\u5408\u6211\u4eec\u7684\u65e5\u5e38\u903b\u8f91\u3002\u73b0\u5728\u95ee\u9898\u6765\u4e86\uff0c\u4f60\u5988\u5988\u5bb6\u91cc\u7684\u7535\u89c6\u673a XiaomiTV1 \u7684\u9065\u63a7\u5668\u88ab\u72d7\u54ac\u574f\u4e86\uff0c\u53eb\u4f60\u53bb\u5546\u573a\u4e70\u4e00\u4e2a\u5c0f\u7c731\u7684\u9065\u63a7\u5668\uff0c\u5e76\u5b9a\u4e49\u4e86\u5982\u4e0b\u6d4b\u8bd5\u63a5\u53e3\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"370\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-3.png\" alt=\"\" class=\"wp-image-1944\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-3.png 844w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-3-300x132.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-3-768x337.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u4f60\u627e\u4e86\u4e00\u5708\uff0c\u4f60\u5728\u5546\u573a\u91cc\u6ca1\u6709\u627e\u5230\u540c\u6837\u578b\u53f7\u7684\u9065\u63a7\u5668\uff08\u5f97\u7f51\u4e0a\u4e70\uff09\uff0c\u4f46\u662f\u4e3a\u4e86\u8ba9\u4f60\u5988\u5988\u7acb\u9a6c\u80fd\u770b\u7535\u89c6\uff0c\u4f60\u5c31\u4e70\u4e86\u4e00\u4e2a\u4e07\u80fd\u9065\u63a7\u5668\u56de\u53bb\uff0c\u60f3\u7740\u65e2\u7136\u662f\u4e07\u80fd\u7684\uff0c\u5e94\u8be5\u662f\u6ca1\u95ee\u9898\u5427\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u4e70\u56de\u6765\u540e\uff0c\u53d1\u73b0\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\uff0c\u8ba9\u4f60\u4e70\u5c0f\u7c73TV1\u7684\u9065\u63a7\u5668\u7ed3\u679c\u4e70\u4e86\u4e2a\u4e07\u80fd\u7684\u9065\u63a7\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"301\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-4-1024x301.png\" alt=\"\" class=\"wp-image-1945\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-4-1024x301.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-4-300x88.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-4-768x225.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-4.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u5e76\u4e0d\u7b26\u5408\u65e5\u5e38\u903b\u8f91\u4e86\uff1f\u4e07\u80fd\u9065\u63a7\u5668\u4e0d\u80fd\u901a\u8fc7\u9065\u63a7\u5668\u6d4b\u8bd5\uff0c\u8981\u4e48\u4f60\u5988\u5988\u65e9\u5c31\u60f3\u5230\u4e86\uff0c\u9700\u8981\u4e00\u4e2a\u4e07\u80fd\u9065\u63a7\u5668\uff0c\u53ef\u4ee5\u8fd9\u4e48\u4ea4\u4ee3\u4f60\uff08\u5b9a\u4e49\u6d4b\u8bd5\u9a8c\u8bc1\u65b9\u6cd5\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"364\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-5.png\" alt=\"\" class=\"wp-image-1946\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-5.png 878w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-5-300x124.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-5-768x318.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u53e6\u5916\u4e00\u79cd\u8fd9\u5c31\u9700\u8981\u7528\u5230\u8303\u578b\u4fee\u9970\u7684\u5173\u952e\u5b57\uff0cin\uff0c\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u91cc\u9700\u8981\u62d3\u5c55\u5230XiaomiTv1\u7684\u7236\u7c7b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"502\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-6.png\" alt=\"\" class=\"wp-image-1947\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-6.png 858w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-6-300x176.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-6-768x449.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u6837\u5c31\u80fd\u901a\u8fc7\u6d4b\u8bd5\u4e86\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u4f7f\u7528in\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u63a5\u53d7\u8fd9\u4e2a\u7c7b\u578b\u7684\u7236\u7c7b\u4f5c\u4e3a\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Out \u53c8\u662f\u4ec0\u4e48\u9b3c\uff1f<\/h2>\n\n\n\n<p>\u518d\u6765\u770b\u4e00\u4e2a\u6817\u5b50\ud83c\udf30\uff1a<\/p>\n\n\n\n<p>\u6211\u4eec\u5047\u5b9aKFC\u662f\u4e00\u79cd\u98df\u7269\uff08\u4e0d\u662f\u4e00\u4e2a\u54c1\u724c\u6216\u8005\u5e97\u94fa\uff09\u6709\u5982\u4e0b\u7ee7\u627f\u5173\u7cfb<\/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=\"\">open class Food {}\nclass KFC : Food() {}<\/pre>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\u5462\uff0c\u6211\u4eec\u8fd8\u6709\u4e00\u4e2a\u996d\u5e97\u7684\u89d2\u8272\uff0c\u53ef\u4ee5\u63d0\u4f9b\u70b9\u5355\u670d\u52a1<\/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=\"\">class Restaurant&lt;T : Food> {\n    fun orderFood(): T {\n        throw NotImplementedError()\n    }\n}<\/pre>\n\n\n\n<p>\u00a0\u8fd9\u91cc\u9700\u8981\u4e00\u5bb6\u666e\u901a\u7684\u996d\u5e97\uff0c\u968f\u4fbf\u4ec0\u4e48\u996d\u5e97\u90fd\u884c<\/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=\"\">\/\/                      \u8fd9\u91cc\u9700\u8981\u4e00\u5bb6\u666e\u901a\u7684\u996d\u5e97\uff0c\u968f\u4fbf\u4ec0\u4e48\u996d\u5e97\u90fd\u884c\n\/\/                                     \u2193\nfun orderFood(restaurant: Restaurant&lt;Food>) {\n    \/\/ \u4ece\u8fd9\u5bb6\u5e97\u70b9\u9910\n    val food = restaurant.orderFood()\n}<\/pre>\n\n\n\n<p>\u8fd9\u65f6\u5019\uff0c\u4f60\u627e\u5230\u4e86\u4e00\u5bb6\u7ecf\u8425\u80af\u5fb7\u57fa\u7684\u5e97\u94fa\uff0c\u4f60\u53d1\u73b0\u65e0\u6cd5\u8ba9\u8fd9\u5bb6\u80af\u5fb7\u57fa\u63d0\u4f9b\u70b9\u9910\u670d\u52a1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"567\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-7-1024x567.png\" alt=\"\" class=\"wp-image-1948\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-7-1024x567.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-7-300x166.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-7-768x425.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-7.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u662f\u4e0d\u662f\u89c9\u5f97\u5f88\u8352\u8c2c\uff1f\u65e2\u7136\u968f\u4fbf\u627e\u4e00\u5bb6\u996d\u5e97\u5c31\u80fd\u70b9\u5355\uff0c\u4e3a\u4ec0\u4e48\u80af\u5fb7\u57fa\u4e0d\u53ef\u4ee5\u5462\uff1f\u7531\u4e8e\u7f16\u8bd1\u5668\u8ba4\u6b7b\u7406\uff0c\u6211\u4eec\u5fc5\u987b\u989d\u5916\u63d0\u4f9b\u4e00\u4e9b\u4fe1\u606f\u7ed9\u7f16\u8bd1\u5668\uff0c\u8ba9\u5b83\u77e5\u9053\u6211\u4eec\u662f\u5728\u7279\u6b8a\u573a\u666f\u4f7f\u7528\u6cdb\u578b\u3002\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u4f20\u5165 out<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"440\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-8.png\" alt=\"\" class=\"wp-image-1949\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-8.png 806w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-8-300x164.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-8-768x419.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u6216\u8005\u4fee\u6539 Restaurant \u7684\u6e90\u4ee3\u7801\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/            \u53d8\u5316\u5728\u8fd9\u91cc\n\/\/                \u2193\nclass Restaurant&lt;out T> {\n    fun orderFood(): T { \/*..*\/ }\n}<\/pre>\n\n\n\n<p>\u5728\u505a\u5b8c\u4ee5\u4e0a\u4efb\u610f\u4e00\u79cd\u4fee\u6539\u4ee5\u540e\uff0c\u4ee3\u7801\u5c31\u53ef\u4ee5\u901a\u8fc7\u7f16\u8bd1\u4e86\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Restaurant&lt;KFC>\u66ff\u4ee3Restaurant&lt;Food>\uff0c\u4e5f\u5c31\u610f\u5473\u7740Restaurant&lt;KFC>\u53ef\u4ee5\u770b\u4f5c\u662fRestaurant&lt;Food>\u7684\u5b50\u7c7b<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u5f53\u6211\u4eec\u62bd\u8c61\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8303\u578b\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528out\uff0c\u5f53\u6211\u4eec\u63a5\u53d7\u4e00\u4e2a\u8303\u578b\u53c2\u6570\u65f6\u6211\u4eec\u53ef\u4ee5\u7528in. \u5728\u5b98\u65b9\u6587\u6863\u4e2d\u6709\u5982\u4e0b\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<p><strong>Consumer in, Producer out!<\/strong>\ud83d\ude42<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"388\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-9-1024x388.png\" alt=\"\" class=\"wp-image-1950\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-9-1024x388.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-9-300x114.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-9-768x291.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-9.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u53e6\u5916\uff0c\u5bf9\u4e8e\u7ffb\u8bd1\uff0ccovariance \u7ffb\u8bd1\u4e3a\u534f\u53d8\uff0ccontravariance \u7ffb\u8bd1\u4e3a\u9006\u53d8\uff0c invariance,\u4e0d\u53d8\uff0cjava\u4e2d\u4e5f\u6709\u5bf9\u5e94\u7684\u6982\u5ff5\u5bf9\u7167\u5982\u4e0b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"194\" src=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-10-1024x194.png\" alt=\"\" class=\"wp-image-1951\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-10-1024x194.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-10-300x57.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-10-768x146.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2024\/07\/image-10.png 1080w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u5e0c\u671b\u770b\u5b8c\u540e\u5bf9kotlin\u591a\u4e00\u5206\u4e86\u89e3\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\u6587\u6863\uff1a&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/generics.html\">https:\/\/kotlinlang.org\/docs\/generics.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/time.geekbang.org\/column\/intro\/100103401\">https:\/\/time.geekbang.org\/column\/intro\/100103401<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u770b\u4e00\u4e9b\u6e90\u7801\u53d1\u73b0\uff0c\u4e00\u4e9b\u6e90\u7801\uff0c\u5728\u8303\u578b\u524d\u9762\u6dfb\u52a0\u4e86\u4e00\u4e9b\u5173\u952e\u5b57 in \u6216\u8005 out\uff0c\u8fd9\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\u6bd4\u5982 kotlin\u5185\u7f6e\u7684\u00a0Comparable.kt \u518d\u6bd4\u5982\uff0cCollections.MutableIterable \u63a5\u53e3 \u4e3a\u4ec0\u4e48\u4f1a\u6709\u4e00\u4e9b\u52a0in\uff0c\u6709\u4e00\u4e9b\u52a0out \u5462\uff1f\u4e86\u89e3\u8fd9\u4e2a\u4e4b\u524d\u6211\u4eec\u6765\u770b\u4e0b \u8303\u578b\u662f\u4ec0\u4e48\uff1f \u6cdb\u578b\uff08Generics\uff09\u662f\u4e00\u79cd\u5141\u8bb8\u7c7b\u3001\u63a5\u53e3\u548c\u65b9\u6cd5\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\uff08\u7c7b\u578b\u53c2\u6570\uff09\u7684\u7279\u6027\u3002\u6cdb\u578b\u63d0\u4f9b\u4e86\u4e00\u79cd\u7c7b\u578b\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5bf9\u8c61\u96c6\u5408\u548c\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u800c\u65e0\u9700\u5728\u7f16\u8bd1\u65f6\u6307\u5b9a\u5177\u4f53\u7684\u7c7b\u578b\u3002 \u4e3e\u4e00\u4e2a\u6817\u5b50\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1935"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1935"}],"version-history":[{"count":7,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1935\/revisions"}],"predecessor-version":[{"id":1953,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1935\/revisions\/1953"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}