{"id":778,"date":"2024-07-21T13:21:23","date_gmt":"2024-07-21T13:21:23","guid":{"rendered":"https:\/\/boochlin.com\/?p=778"},"modified":"2025-11-12T12:17:47","modified_gmt":"2025-11-12T12:17:47","slug":"lateinit-vs-lazy","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=778","title":{"rendered":"lateinit vs lazy"},"content":{"rendered":"<\/p>\n<p><strong>lateinit\uff1avar<\/strong> <strong>\u7d66\u5225\u4eba\u521d\u59cb\u5316\u7684\u627f\u8afe<\/strong> \u6700\u9069\u5408\u7528\u5728\u751f\u547d\u9031\u671f\u6216\u4f9d\u8cf4\u6ce8\u5165\uff0c\u7576\u4f60\u78ba\u5b9a\u67d0\u500b\u5916\u90e8\u529b\u91cf\u6703\u5e6b\u4f60\u641e\u5b9a\u521d\u59cb\u5316\u6642\uff1a<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">lateinit var adapter: RecyclerView.Adapter\n\nfun onCreate(savedInstanceState: Bundle?) {\n\u00a0 \u00a0 super.onCreate(savedInstanceState)\n\u00a0 \u00a0 adapter = MyAdapter(data)\u00a0\n}\n<\/pre>\n<p><strong>lazy\uff1aval\u628a\u6602\u8cb4\u7684\u8a08\u7b97\u5f80\u5f8c\u63a8<\/strong> \u7528\u5728\u6210\u672c\u9ad8\u6602\u3001\u53c8\u4e0d\u4e00\u5b9a\u6703\u7528\u5230\u7684\u552f\u8b80\u8cc7\u6e90\uff0c\u628a\u904b\u7b97\u62d6\u5230\u6700\u5f8c\u4e00\u523b\uff1a<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ \u9019\u5f35\u5716\u8d85\u5927\uff0c\u6c92\u4eba\u8981\u770b\u5c31\u5225\u6d6a\u8cbb\u8a18\u61b6\u9ad4\u8f09\u5165\nval profileBitmap: Bitmap by lazy {\n\u00a0 \u00a0 Log.d(\"Perf\", \"Loading large bitmap NOW!\")\n\u00a0 \u00a0 BitmapFactory.decodeResource(resources, R.drawable.large_image)\n}\n<\/pre>\n<p>\u4e00\u500b\u662f\u300c<strong>\u4e4b\u5f8c\u6703\u6709\u4eba\u641e\u5b9a<\/strong>\u300d\u7684 var\uff0c\u4e00\u500b\u662f\u300c<strong>\u6211\u53ea\u5728\u5fc5\u8981\u6642\u7b97\u4e00\u6b21<\/strong>\u300d\u7684 val\u3002\u5225\u518d\u7528\u932f\u5730\u65b9\uff0c\u6d6a\u8cbb\u90a3\u5bf6\u8cb4\u7684\u8cc7\u6e90\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lateinit\uff1avar \u7d66\u5225\u4eba\u521d\u59cb\u5316\u7684\u627f\u8afe \u6700\u9069\u5408\u7528\u5728\u751f\u547d\u9031\u671f\u6216\u4f9d\u8cf4\u6ce8\u5165\uff0c\u7576\u4f60\u78ba\u5b9a\u67d0\u500b\u5916\u90e8\u529b\u91cf\u6703\u5e6b\u4f60\u641e\u5b9a\u521d\u59cb\u5316\u6642\uff1a Kotlin lateinit var adapter: RecyclerView&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/778"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=778"}],"version-history":[{"count":3,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":789,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions\/789"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}