vault backup: 2022-09-30 21:53:03
Affected files: .obsidian/workspace 03. 專注Study/Android/ADB 取得 APK 的 icon.md 03. 專注Study/Android/ADB.md 03. 專注Study/Android/AOSP.md 03. 專注Study/Android/Android programming.md 03. 專注Study/Android/AudioTrack.md 03. 專注Study/Android/Ktor.md 03. 專注Study/Android/Service.md 03. 專注Study/Android/Tools.md 03. 專注Study/Android/UI.md 03. 專注Study/C++/C++17.md 03. 專注Study/C++/Class template.md 03. 專注Study/C++/GCC.md 03. 專注Study/C++/Structured binding declaration.md 03. 專注Study/C++/for_each.md 03. 專注Study/C++/lambda.md 03. 專注Study/C++/lvalue.md 03. 專注Study/C++/move operator.md 03. 專注Study/C++/rvalue.md 03. 專注Study/C++/智慧指標.md 03. 專注Study/RxKotlin/20200207 - Study RxKotlin.md 04. Programming/COM/20210726 - COM Interface.md 04. Programming/DB/MySQL.md 04. Programming/DB/sqlite.md 04. Programming/Design Pattern.md 04. Programming/FFMPEG/00. Introduction.md 04. Programming/FFMPEG/01. Setup.md 04. Programming/FFMPEG/FFMpeg.md 04. Programming/Flask.md 04. Programming/Kotlin/class.md 04. Programming/Kotlin/run, let, with, also 和 apply.md 04. Programming/Media Foundation/20210604 - Windows media foundation.md 04. Programming/OpenCV.md 04. Programming/OpenGL.md 04. Programming/Python/argparse.ArgumentParser.md 04. Programming/Python/decorator.md 04. Programming/Python/logging.md 04. Programming/Python/opencv.md 04. Programming/Python/subprocess.md 04. Programming/Python/threading.md 04. Programming/Python/tkinter.md 04. Programming/Python/檢測工具.md 04. Programming/QT/Dropdown button.md 04. Programming/QT/QVariant.md 04. Programming/QT/Qt.md 04. Programming/UML.md 04. Programming/演算法.md 05. 資料收集/99. templates/blogHeader.md 05. 資料收集/99. templates/date.md 05. 資料收集/99. templates/front matter.md 05. 資料收集/99. templates/note.md 05. 資料收集/99. templates/table.md 05. 資料收集/99. templates/thisWeek.md 05. 資料收集/99. templates/日記.md 05. 資料收集/99. templates/讀書筆記.md 05. 資料收集/Linux/CLI/cut.md 05. 資料收集/Linux/CLI/scp.md 05. 資料收集/Linux/CLI/timedatectl.md 05. 資料收集/Linux/Programming.md 05. 資料收集/Linux/Ubuntu.md 05. 資料收集/Tool Setup/Hardware/RaspberryPi.md 05. 資料收集/Tool Setup/Software/Chrome.md 05. 資料收集/Tool Setup/Software/Obisidian.md 05. 資料收集/Tool Setup/Software/SublimeText.md 05. 資料收集/Tool Setup/Software/VirtualBox.md 05. 資料收集/Tool Setup/Software/Visual Studio Code.md 05. 資料收集/Tool Setup/Software/Windows Setup.md 05. 資料收集/Tool Setup/Software/Windows Terminal.md 05. 資料收集/Tool Setup/Software/freefilesync.md 05. 資料收集/Tool Setup/Software/vim.md 05. 資料收集/名言佳句.md 05. 資料收集/架站/Gitea.md 05. 資料收集/架站/HTTP Server/Apache.md 05. 資料收集/架站/HTTP Server/Nginx/Reverse Proxy(Layer4).md 05. 資料收集/架站/Pelican blog.md 05. 資料收集/架站/Proxmox VE.md 05. 資料收集/架站/SWAG Reverse proxy.md 05. 資料收集/架站/Storj.md 05. 資料收集/架站/Trojan.md 05. 資料收集/每週外食.md 05. 資料收集/科技/802.11.md 05. 資料收集/科技/HDR Sensor.md 05. 資料收集/科技/量子電腦.md 05. 資料收集/科技/鋰電池.md 05. 資料收集/興趣嗜好/RC/Traxxas Sledge.md 05. 資料收集/興趣嗜好/RC/好盈電變調整中立點.md 05. 資料收集/興趣嗜好/RC/差速器調教教學.md 05. 資料收集/興趣嗜好/模型/舊化作例.md 05. 資料收集/興趣嗜好/軍武/虎式.md 05. 資料收集/讀書筆記/20201201 - 學習如何學習.md 05. 資料收集/讀書筆記/20201218 - Kotlin權威2.0.md 05. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md 05. 資料收集/讀書筆記/20210119 - 中產悲歌.md 05. 資料收集/讀書筆記/20210220 - 最高學習法.md 05. 資料收集/讀書筆記/20210320 - 最高學以致用法.md 05. 資料收集/讀書筆記/20210406 - 精準購買.md 05. 資料收集/讀書筆記/20210723 - 高手學習.md 05. 資料收集/讀書筆記/20220526 - 深入淺出設計模式.md 05. 資料收集/讀書筆記/20220619 - 精確的力量.md 05. 資料收集/軟體工具/IPFS.md 05. 資料收集/軟體工具/MkDocs.md 05. 資料收集/軟體工具/Obsidian.md 05. 資料收集/軟體工具/docker.md 05. 資料收集/軟體工具/git/apply.md 05. 資料收集/軟體工具/git/submodule.md 05. 資料收集/軟體工具/youtube-dl.md 05. 資料收集/面試準備/技术面试最后反问面试官的话.md
This commit is contained in:
62
04. Programming/Kotlin/class.md
Normal file
62
04. Programming/Kotlin/class.md
Normal file
@@ -0,0 +1,62 @@
|
||||
## 最簡單 class 定義
|
||||
Kotlin 中的 class 由 `class` 關鍵字開始,一個簡單的class如下:
|
||||
```kotlin
|
||||
class VerySimple {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
如果需要 constructor 的話,則在 class 名稱之後加入所需的參數,如下:
|
||||
```kotlin
|
||||
class VerySimple(val para1: Int, val para2: String ) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
加在 constructor 中的參數會自動變成 class 的「成員變數」,如果在參數前面加上 `private`,則會成「私有成員變數」,也就是無法被外部所存取。
|
||||
|
||||
## 多個 constructor
|
||||
前面所的 constructor 是建立class 的「主要constructor」,kotlin 也允許建立其他 constructor,但是這些「次要constructor」都必須呼叫「主要constructor」來進行初始化,如下:
|
||||
```kotlin
|
||||
class VerySimple(val para1: Int, val para2: String ) {
|
||||
constructor(val para1: Int): this(para1, para2="someText") // 第一個「次要constructor」
|
||||
constructor(val para2: String): this(para1=123, para2) // 第二個「次要constructor」
|
||||
}
|
||||
```
|
||||
|
||||
不管是第一個「次要constructor」或是第二個「次要constructor」都必須呼叫主要的`(val para1: Int, val para2: String )`這一個「主要constructor」。
|
||||
|
||||
如果有邏輯上的需求,「次要constructor」也可以加上邏輯判斷區塊,如下:
|
||||
```kotlin
|
||||
class VerySimple(val para1: Int, val para2: String ) {
|
||||
constructor(val para1: Int): this(para1, para2="someText") // 第一個「次要constructor」
|
||||
constructor(val para2: String): this(para1=123, para2) { // 第二個「次要constructor」
|
||||
if (para2 == "error") {
|
||||
println("Something wrong")
|
||||
para1 = 25 // 這行會錯誤,因為para1是被宣告成val,而不是var
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `init` 區塊
|
||||
`init`是個優先權高於「主要constructor」的執行區塊,如下:
|
||||
```kotlin
|
||||
class VerySimple(val para1: Int, val para2: String ) {
|
||||
|
||||
init {
|
||||
require(para1 > 0, {"para1 must larger than 0"})
|
||||
require(para2.isNotBlank(), {"para2 cannot be a empty string"})
|
||||
}
|
||||
|
||||
constructor(val para1: Int): this(para1, para2="someText") // 第一個「次要constructor」
|
||||
constructor(val para2: String): this(para1=123, para2) { // 第二個「次要constructor」
|
||||
if (para2 == "error") {
|
||||
println("Something wrong")
|
||||
para1 = 25 // 這行會錯誤,因為para1是被宣告成val,而不是var
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`init`區塊會在初始化之前進行檢查,如果不符合條件,則會丟出`IllegalArgumentException`異常。
|
||||
80
04. Programming/Kotlin/run, let, with, also 和 apply.md
Normal file
80
04. Programming/Kotlin/run, let, with, also 和 apply.md
Normal file
@@ -0,0 +1,80 @@
|
||||
`run`, `let`, `with`, `also` 和 `apply` 這幾個都是可以搭配 object 使用的函數,它們之間的差異不大,主要是讓程式在語意上更加流暢。
|
||||
以下解釋各個的差別。
|
||||
|
||||
## 歸納
|
||||
| | 變數名稱 | 自訂變數名稱 | 回傳 |
|
||||
|:--------------:|:----------:|:------------:|:--------:|
|
||||
| `run()` | | | 最後一行 |
|
||||
| `with()` | `this` | No | 最後一行 |
|
||||
| `object.run` | `this` | No | 最後一行 |
|
||||
| `object.let` | `it` | Yes | 最後一行 |
|
||||
| `object.also` | `it` | Yes | `this` |
|
||||
| `object.apply` | `this` | No | `this` |
|
||||
|
||||
## 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]]是一樣的,也是**回傳「最後一行」**,只是 `object.run` 是讓 object 呼叫的,而且 lambda scope 中的物件會變成 `this`,`this` 可以省略,但是不可以自訂變數名稱,如下:
|
||||
```kotlin
|
||||
val anObject = MyObject()
|
||||
anObject.run {
|
||||
this.doSomething() // this就是anObject,this可以省略
|
||||
}
|
||||
```
|
||||
|
||||
## with
|
||||
`with(T)` 之中的傳入值可以以 `this` (稱作 identifier) 在 scope 中取用,不用打出 `this`也沒關係。雖然, `with` 也會**將最後一行回傳**。但是因為 `with` 沒有先判斷物件是否為 `null`,所以 scope 中還是要用`?`來判斷是否 `null`,如下:
|
||||
```kotlin
|
||||
val anObject = makeMyObject() // anObject有可能是null
|
||||
with(anObject) {
|
||||
this?.doSomething() // this就是anObject,this可能是null
|
||||
}
|
||||
```
|
||||
|
||||
## object.let
|
||||
`let` 的 scope 裡,物件是 `it`,可以自訂變數名稱,一樣會**回傳最後一行**。如下:
|
||||
```kotlin
|
||||
val anObject = makeMyObject()
|
||||
anObject?.let { // anObject有可能是null,所以要先用?判斷
|
||||
it.doSomething() // it就是anObject,在scope內一定不是null
|
||||
}
|
||||
```
|
||||
|
||||
## object.also
|
||||
`also` 的 scope 裡,物件是 `it`,可以自訂變數名稱,但是不是回傳最後一行,是**回傳自己**(也就是 `it`)。如下:
|
||||
```kotlin
|
||||
val anObject = makeMyObject()
|
||||
anObject?.also { // anObject有可能是null,所以要先用?判斷
|
||||
it.doSomething() // it就是anObject,在scope內一定不是null
|
||||
}.also {
|
||||
it.doSomething2() // it就是anObject
|
||||
}.also {
|
||||
it.doSomething3() // it就是anObject
|
||||
}
|
||||
```
|
||||
|
||||
## object.apply
|
||||
`apply` 的 scope 裡,物件是 `this`,不可以自訂變數名稱,但是不是回傳最後一行,是**回傳自己**(也就是 `this`)。如下:
|
||||
```kotlin
|
||||
val anObject = makeMyObject()
|
||||
anObject?.also { // anObject有可能是null,所以要先用?判斷
|
||||
this.doSomething() // this就是anObject,在scope內一定不是null
|
||||
}.also {
|
||||
this.doSomething2() // this就是anObject
|
||||
}.also {
|
||||
this.doSomething3() // this就是anObject
|
||||
}
|
||||
```
|
||||
|
||||
## 參考
|
||||
- [簡介 Kotlin: run, let, with, also 和 apply](https://louis383.medium.com/%E7%B0%A1%E4%BB%8B-kotlin-run-let-with-also-%E5%92%8C-apply-f83860207a0c)
|
||||
- [Kotlin 的 scope function: apply, let, run..等等](https://jchu.cc/2018/05/05-kotlin.html)
|
||||
- [Kotlin 線上讀書會 筆記 (五) apply、let、run、with、also和takeIf](https://medium.com/evan-android-note/kotlin-%E7%B7%9A%E4%B8%8A%E8%AE%80%E6%9B%B8%E6%9C%83-%E7%AD%86%E8%A8%98-%E4%BA%94-apply-let-run-with-also%E5%92%8Ctakeif-2c09d42b09b5)
|
||||
Reference in New Issue
Block a user