{"id":804,"date":"2017-08-16T22:39:00","date_gmt":"2017-08-16T14:39:00","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=804"},"modified":"2021-07-21T10:40:29","modified_gmt":"2021-07-21T02:40:29","slug":"androidkotlin%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%984-lateinit%e8%b7%9flazy","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=804","title":{"rendered":"[Android]Kotlin\u5b78\u7fd2\u7b46\u8a18#5-lateinit\u8ddflazy"},"content":{"rendered":"\n<p>\u7531\u65bcKotlin\u7684Null Safety\u7279\u6027\uff0c\u5ba3\u544a\u8b8a\u6578\u6642\u4e00\u5b9a\u8981\u521d\u59cb\u5316\uff0c\u5426\u5247\u7de8\u8b6f\u5668\u6703\u5831\u932f\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u6211\u5011\u5728\u5ba3\u544a\u8b8a\u6578\u7684\u6642\u5019\u6703\u7528\u4e0b\u9762\u4e09\u7a2e\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">var text1 = \"text1\" \/\/ \u7701\u7565\u578b\u5225\nvar text2: String = \"text2\" \/\/ \u4e0d\u7701\u7565\u578b\u5225\nvar text3: String? = null \/\/ \u578b\u5225\u70ba String \u4e14\u53ef\u70ba\u7a7a<\/code><\/pre>\n\n\n\n<p>\u4f46\u6709\u6642\u5019\u6211\u4e26\u4e0d\u60f3\u8b93\u8b8a\u6578nullable\uff0c\u800c\u4e14\u6211\u4e5f\u6c92\u8fa6\u6cd5\u8b93\u4ed6\u4e00\u958b\u59cb\u5c31\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u9019\u6642\u5019\u5c31\u53ef\u4ee5\u7528lateinit\u6216lazy\u4f86\u505a\u5ef6\u9072\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">lateinit var<\/h3>\n\n\n\n<p>\u7576\u8981\u7d81\u5b9a\u4e00\u500bUI\u5143\u4ef6\u5230\u7a0b\u5f0f\u78bc\uff0c\u4e14\u9019\u500b\u985e\u5225\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u8981\u80fd\u5920\u4f7f\u7528\u4ed6\u7684\u6642\u5019\uff0c\u5fc5\u9808\u5ba3\u544a\u4e00\u500b\u985e\u5225\u5167\u90e8\u7684\u5168\u57df\u8b8a\u6578\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class MainActivity : AppCompatActivity() {\n\n    private var textView: TextView? = null\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n        textView = findViewById(R.id.textView)\n        if(textView != null) {\n            textView.text = \"\u6211\u597d\u5e25\"\n        }\n     }\n }<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u9019\u500btextView\u7269\u4ef6\u662fnullable\u7684\uff0c\u5982\u679c\u5fd8\u8a18\u8ce6\u503c\u7684\u6642\u5019\uff0c\u6703\u767c\u751f\u7a7a\u503c\u4f8b\u5916\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u5728\u5c0d\u4ed6\u9032\u884c\u64cd\u4f5c\u7684\u6642\u5019\uff0c\u5fc5\u9808\u5148\u6aa2\u67e5\u662f\u5426\u70banull\uff0c\u8c48\u4e0d\u662f\u5f88\u9ebb\u7169\uff1f<\/p>\n\n\n\n<p>\u56e0\u6b64Kotlin\u63d0\u4f9b\u4e86lateinit\u9019\u500b\u4fee\u98fe\u8a5e\u8b93var\u53ef\u4ee5\u5ef6\u9072\u521d\u59cb\u5316\uff0c\u5982\u6b64\u4e00\u4f86textView\u9019\u500b\u7269\u4ef6\u5c31\u4e0d\u662f\u518d\u662f\u53ef\u70ba\u7a7a\u7684\u8b8a\u6578\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">class MainActivity : AppCompatActivity() {\n    private lateinit var textView: TextView\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n        textView = findViewById(R.id.textView)\n        textView.text = \"\u6211\u597d\u5e25\"\n     }\n }<\/code><\/pre>\n\n\n\n<p>\u4f46\u5fc5\u9808\u6ce8\u610f\uff0c\u4f7f\u7528\u5ef6\u9072\u521d\u59cb\u5316\u5ba3\u544a\u8b8a\u6578\u6642\u4e00\u5b9a\u8981\u5728\u4f7f\u7528\u524d\u5148\u5c07\u7269\u4ef6\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u5426\u5247\u53ef\u662f\u6703\u8df3\u51fa&#8221;lateinit property has not been initialized&#8221;\u7684\u932f\u8aa4\u8a0a\u606f\u5462\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">by lazy<\/h3>\n\n\n\n<p>\u524d\u9762\u63d0\u5230\u4e86\u8b8a\u6578\u53ef\u4ee5\u7528lateinit var\u4f86\u5ba3\u544a\uff0c\u4f7f\u5176\u53ef\u4ee5\u5ef6\u9072\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u90a3\u5e38\u6578\u7684\u90e8\u5206\uff0c\u5247\u662f\u53ef\u4ee5\u900f\u904e<code>by lazy<\/code>\u9019\u500b\u8a9e\u6cd5\u4f86\u5c07\u5e38\u6578\u8b8a\u6578\u5ef6\u9072\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528by lazy\u65b9\u5f0f\u521d\u59cb\u5316\u7684\u5e38\u6578\u7269\u4ef6\uff0c\u5728\u7b2c\u4e00\u6b21\u5f15\u7528\u6642\uff0c\u624d\u6703\u9032\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">private val text1 by lazy { \"Text1\" }<\/code><\/pre>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0clazy\u4e5f\u80fd\u7528\u5728\u65b9\u6cd5\u5167\u7684\u5340\u57df\u8b8a\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">fun myFunction() {\n    val text by lazy { \"text\" }\n    println(text)\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u5ef6\u9072\u521d\u59cb\u5316\u7684\u7b46\u8a18\uff0c\u5927\u81f4\u4e0a\u662f\u9019\u6a23\u3002<\/p>\n\n\n\n<p>\u4e4b\u5f8c\u5982\u679c\u6709\u5176\u4ed6\u5fc3\u5f97\u6703\u518d\u7e7c\u7e8c\u88dc\u5145\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u65bcKotlin\u7684Null Safety\u7279\u6027\uff0c\u5ba3\u544a\u8b8a\u6578\u6642\u4e00\u5b9a\u8981\u521d\u59cb\u5316\uff0c\u5426\u5247\u7de8\u8b6f\u5668\u6703\u5831\u932f\u3002 \u4e00\u822c\u6211\u5011\u5728\u5ba3\u544a\u8b8a\u6578\u7684 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,19],"class_list":["post-804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-android-app","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":4,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":996,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}