DAY 4
1
Mobile Development

• 類別
• 繼承
• 介面
• 擴展
• 泛型

# 類別

## 定義一個 Calc 類別

``````class Calc {
fun sum(x: Int, y: Int) {
println(x + y)//加法運算
}
fun substract(x: Int, y: Int) {
println(x - y)//減法運算
}
fun multiply(x: Int, y: Int) {
println(x * y) //乘法運算
}
fun divide(x: Int, y: Int) {
println(x / y)//除法運算
}
}
``````

## 使用類別的方式

``````fun main() {
//實體化
val calc = Calc()

calc.sum(1, 1) //2
calc.substract(1, 1)//0
calc.multiply(1, 1)//1
calc.divide(1, 1)//1
}
``````

## 輸出結果

``````2
0
1
1
``````

# 存取類別的屬性

## 定義一個 Account 類別

``````class Account {
//帳號
var id: String = ""
//戶名
var name: String = ""
//餘額
var balance: String = ""
}
``````

## 存取類別屬性方式

``````fun main() {
//實體化
val account = Account()

//設定類別屬性值
account.id = "123456789"
account.name = "HKT"
account.balance = "100"

//獲取類別屬性值
println(account.id)
println(account.name)
println(account.balance)
}

``````

## 輸出結果

``````123456789
HKT
100
``````

# 修改 getter 與 setter 方法

``````class Account {
//戶名
var name: String = ""
get() = field.toUpperCase()
set(value) {
if (value == "") {
field = "戶名不能為空"
} else {
field = value
}
}
}
``````
``````fun main() {
//實體化
val account = Account()

account.name = "HKT"
println(account.name)

account.name = ""
println(account.name)
}
``````

## 輸出結果

``````HKT

``````

# 類別初始化

``````class Account {
//戶名
var name: String = ""

init {
println("初始化，第一層")
}

constructor() {
println("初始化，第二層")
}
}
``````
``````fun main() {
//實體化
val account = Account()

account.name = "HKT"
println(account.name)}
}
``````

## 輸出結果

``````初始化，第一層

HKT
``````

# 繼承

## 定義一個父類別 Person

``````open class Person {
//戶名
var name: String = ""
//地址
}
``````

## 定義一個子類別 Account 並且去繼承 Person 父類別

``````class Account : Person() {
//帳號
var id: String = ""
}
``````

## 繼承使用範例

``````fun main() {
//實體化
val account = Account()

account.id = "123456789"
account.name = “HKT"

println(account.id)
println(account.name)
}
``````

## 輸出結果

``````123456789
HKT

``````

# 繼承後覆寫屬性

``````open class Person {
//戶名
open var name: String = ""
}
``````

``````class Account : Person() {
//覆寫戶名
override var name: String = ""
}
``````

## 繼承後覆寫屬性方式

``````fun main() {
//實體化
val account = Account()

account.name = "HKT"

println(account.name)
}

``````

## 輸出結果

``````HKT
``````

# 繼承後覆寫函數

``````open class Person {
open fun showName() {
println("HKT")
}
}
``````

``````class Account : Person() {
override fun showName() {
println("Kitty")
}
}
``````

## 繼承後覆寫範例

``````fun main() {
//實體化
val account = Account()

account.showName()
}
``````

## 輸出結果

``````Kitty
``````

# 介面

## 定義一個的介面

``````interface PersonInterface {
fun showName()
}
``````

## 實作 Interface 要求的 showName

``````class Account : PersonInterface {
override fun showName() {
println("name: HKT")
}
}
``````

## 實際使用介面方式

``````fun main() {
//實體化
val account = Account()

account.showName()
}
``````

## 輸出結果

``````name: HKT
``````

# 擴展

## 定義一個 Account 類別

``````class Account {
var id: String = ""
}
``````

## 擴展 Account ，加入新功能

``````fun Account.Print() {
println("帳號: " + id)
}
``````

## 實際使用擴展方式

``````fun main() {
//實體化
val account = Account()
account.id = "123456789"
account.Print()
}
``````

## 輸出結果

``````帳號: 123456789
``````

# 泛型

## 泛型關鍵字： Ｔ

``````class Box<T>(t: T) {
var value = t
}
``````

## 泛型使用範例

``````fun main() {
val box_int = Box(1)
println(box_int.value)

val box_string = Box("我是字串")
println(box_string.value)
}

``````

## 輸出結果

``````1

``````

## 參考資料

HKT 線上教室
https://tw-hkt.blogspot.com/

Freepik
https://www.freepik.com/

Kotlin docs
https://kotlinlang.org/docs/home.html