Jetpack Compose 教學上課講義【從零開始學 Jetpack Compose 程式設計】Kotlin 語法補充篇 - Builder Pattern 建造者模式
【從零開始學 Jetpack Compose 程式設計】
線上教學課程目錄: https://bit.ly/3JF4SFA
Youtube 課程播放清單:https://bit.ly/3tFjRbx
Udemy 線上課程:https://bit.ly/3MbVnhO
【從零開始學 Jetpack Compose 程式設計】
線上教學課程目錄: https://bit.ly/3JF4SFA
Youtube 課程播放清單:https://bit.ly/3tFjRbx
Udemy 線上課程:https://bit.ly/3MbVnhO
Builder Pattern 建造者模式
package com.example.myapplication
class Account private constructor(
val id: String?,
val name: String?,
val balance: String?,
) {
data class Builder(
var id: String? = null,
var name: String? = null,
var balance: String? = null,
) {
fun id(id: String) = apply { this.id = id }
fun name(name: String) = apply { this.name = name }
fun balance(balance: String) = apply { this.balance = balance }
fun build() = BankAccount(id, name, balance)
}
}
fun main() {
val account = Account.Builder()
.id("123456789")
.name("HKT線上教室")
.balance("666")
.build()
println(bankAccount.id)
println(bankAccount.name)
println(bankAccount.balance)
}