{"id":778,"date":"2024-07-21T13:21:23","date_gmt":"2024-07-21T13:21:23","guid":{"rendered":"https:\/\/boochlin.com\/?p=778"},"modified":"2026-09-19T17:04:05","modified_gmt":"2026-09-19T17:04:05","slug":"lateinit-vs-lazy","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=778","title":{"rendered":"lateinit vs lazy"},"content":{"rendered":"<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<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-keyword\">lateinit<\/span> <span class=\"hljs-keyword\">var<\/span> adapter: RecyclerView.Adapter\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">onCreate<\/span><span class=\"hljs-params\">(savedInstanceState: <span class=\"hljs-type\">Bundle<\/span>?)<\/span><\/span> {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">super<\/span>.onCreate(savedInstanceState)\n\u00a0 \u00a0 adapter = MyAdapter(<span class=\"hljs-keyword\">data<\/span>)\u00a0\n}<\/code><\/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<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-comment\">\/\/ \u9019\u5f35\u5716\u8d85\u5927\uff0c\u6c92\u4eba\u8981\u770b\u5c31\u5225\u6d6a\u8cbb\u8a18\u61b6\u9ad4\u8f09\u5165<\/span>\n<span class=\"hljs-keyword\">val<\/span> profileBitmap: Bitmap <span class=\"hljs-keyword\">by<\/span> lazy {\n\u00a0 \u00a0 Log.d(<span class=\"hljs-string\">&quot;Perf&quot;<\/span>, <span class=\"hljs-string\">&quot;Loading large bitmap NOW!&quot;<\/span>)\n\u00a0 \u00a0 BitmapFactory.decodeResource(resources, R.drawable.large_image)\n}<\/code><\/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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[192,249],"_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":6,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":1548,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/778\/revisions\/1548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1228"}],"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}]}}