Files
Obsidian-Main/03. Programming/Kotlin/class.md
Awin Huang cce530515c vault backup: 2022-09-30 11:34:07
Affected files:
03. Programming/Kotlin/class.md
2022-09-30 11:34:07 +08:00

17 lines
747 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Kotlin中的class由class關鍵字開始一個簡單的class如下
class VerySimple {
}
如果需要constructor的話則在class名稱之後加入所需的參數如下
class VerySimple(val para1: Int, val para2: String ) {
}
加在constructor中的參數會自動變成class的「成員變數」如果在參數前面加上private則會成「私有成員變數」也就是無法被外部所存取。
## 多個 constructor
前面所的constructor是建立class的kotlin也允許建立其他constructor但是這些「次要constructor」都必須呼叫「主要constructor」來進行初始化如下
class VerySimple(val para1: Int, val para2: String ) {
constructor(val para1: Int): this(para1, para2=)
}