From 5ee739faf9103389ebf6d8eb948295691d8d9e4b Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Fri, 30 Sep 2022 21:13:03 +0800 Subject: [PATCH] vault backup: 2022-09-30 21:13:03 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: .obsidian/workspace 03. Programming/Kotlin/class.md 03. Programming/Kotlin/run, let, with, also 和 apply.md --- .obsidian/workspace | 10 ++++----- 03. Programming/Kotlin/class.md | 2 +- .../Kotlin/run, let, with, also 和 apply.md | 22 +++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 03. Programming/Kotlin/run, let, with, also 和 apply.md diff --git a/.obsidian/workspace b/.obsidian/workspace index 70c9d1e..b6e66c5 100644 --- a/.obsidian/workspace +++ b/.obsidian/workspace @@ -9,7 +9,7 @@ "state": { "type": "markdown", "state": { - "file": "03. Programming/Kotlin/class.md", + "file": "03. Programming/Kotlin/run, let, with, also 和 apply.md", "mode": "source", "source": true } @@ -69,7 +69,7 @@ "state": { "type": "backlink", "state": { - "file": "03. Programming/Kotlin/class.md", + "file": "03. Programming/Kotlin/run, let, with, also 和 apply.md", "collapseAll": false, "extraContext": false, "sortOrder": "alphabetical", @@ -86,7 +86,7 @@ "state": { "type": "outline", "state": { - "file": "03. Programming/Kotlin/class.md" + "file": "03. Programming/Kotlin/run, let, with, also 和 apply.md" } } } @@ -116,6 +116,7 @@ }, "active": "828beb43bb437dd1", "lastOpenFiles": [ + "03. Programming/Kotlin/class.md", "01. 個人/02. 專注Study/RxKotlin/20200207 - Study RxKotlin.md", "01. 個人/01. Daily/2021/08/2021-08-16(週一).md", "01. 個人/01. Daily/2021/08/2021-08-14(週六).md", @@ -124,7 +125,6 @@ "01. 個人/01. Daily/2020/12/2020-12-11(Fri).md", "01. 個人/01. Daily/2020/12/2020-12-09(Wed).md", "01. 個人/01. Daily/2018/2018-10-12(週五).md", - "00. Inbox/00. Inbox/A cheatsheet of modern C++ language and library features.md", - "00. Inbox/00. Inbox/Modern C++ use in Chromium.md" + "00. Inbox/00. Inbox/A cheatsheet of modern C++ language and library features.md" ] } \ No newline at end of file diff --git a/03. Programming/Kotlin/class.md b/03. Programming/Kotlin/class.md index 05afdad..0a0c1c0 100644 --- a/03. Programming/Kotlin/class.md +++ b/03. Programming/Kotlin/class.md @@ -39,7 +39,7 @@ class VerySimple(val para1: Int, val para2: String ) { } ``` -## init區塊 +## `init` 區塊 `init`是個優先權高於「主要constructor」的執行區塊,如下: ```kotlin class VerySimple(val para1: Int, val para2: String ) { diff --git a/03. Programming/Kotlin/run, let, with, also 和 apply.md b/03. Programming/Kotlin/run, let, with, also 和 apply.md new file mode 100644 index 0000000..76eca98 --- /dev/null +++ b/03. Programming/Kotlin/run, let, with, also 和 apply.md @@ -0,0 +1,22 @@ +run, let, with, also 和 apply 這幾個都是可以搭配object 使用的函數,它們之間的差異不大,主要是讓程式看起來更符合語意。 +以下解釋各個的差別。 + +## run +run後面的區塊會回傳「最後一行」,所以可以進行「串接」。如下: +```kotlin +run { + val telephone = Telephone() + telephone.whoCallMe = "English" + telephone // <-- telephone 被帶到下一個 Chain +}.callMe("Softest part of heart") // <-- 這裡可以執行 `Telephone` Class 的方法 +``` + +## object.run +object.run跟[[run, let, with, also 和 apply#run]]是一樣的,只是object.run是讓object呼叫的,而且lambda scope中的物件會變成this,如下: +```kotlin +val anObject = MyObject() +anObject.run { + this.doSomething() // this就是anObject +} +``` +