1. ์ง์ฐ ์ด๊ธฐํ
: ํด๋์ค ์ค๊ณ ์ ์ด๊ธฐ์ ๊ฐ ์ค์ ํ๊ธฐ ์ ๋งคํ ๋, ๋์ค์ ๋์
(+) ๋ฉ๋ชจ๋ฆฌ์ ํจ์จ์ ์ธ ์ฌ์ฉ
1) ๋ณ์ --> lateinit
- isInitialized --> ๋ณ์ ์ฌ์ฉ ์ , ์ด๊ธฐํ๋์๋์ง ํ์ธํด true/false ๋ฐํ ( !::name.isInitialized)
- ๊ฐ์ด ์๋๋ผ ์ฐธ์กฐํํ๋ก ์ฌ์ฉํด์ผ ํ๊ธฐ์ this::~ ๋๋ :: ๋ถ์
// var์๋ง ์ฌ์ฉ ๊ฐ๋ฅ, ํ๋กํผํฐ์ ๋ํ ๊ฒํฐ, ์ธํฐ๋ฅผ ์ฌ์ฉํ ์ ์์(์ด๊ธฐํ๋ ๋ค์๋ ์์ ๊ฐ๋ฅํ๊ธด ํ ๋ฏ)
fun main(){
var s1 = Student()
s1.name = "์ฐธ์" //์ด ๋ ์ด๊ธฐํ๋จ
s1.displayInfo()
s1.age = 10
s1.displayInfo()
}
class Student {
lateinit var name:String // ์ฌ๊ธฐ
var age:Int = 0
fun displayInfo() {
if(this::name.isInitialized) { //์ฌ๊ธฐ
println("์ด๋ฆ์: ${name} ์
๋๋ค.")
println("๋์ด๋: ${age} ์
๋๋ค.")
} else {
println("name๋ณ์๋ฅผ ์ด๊ธฐํํด์ฃผ์ธ์.")
}
}
}
2) ์์ --> lazy
- ์ฌ์ฉ ์์ ์ ๊ฐ์ ๋์ ํ๊ณ ์ด๊ธฐํ๋จ
fun main(){
var s1 = Student()
s1.name = "์ฐธ์"
s1.displayInfo() //์ด ๋ ํ๋ฒ ์ด๊ธฐํ๋จ, address ์ด๊ธฐํ ์ถ๋ ฅ๋ ํ ์ฃผ์๋ : ~ ์ถ๋ ฅ๋จ
s1.age = 10
s1.displayInfo() //์ด ๋ address ์ด๊ธฐํ ์ถ๋ ฅ๋์ง ์์
}
class Student {
lateinit var name:String
var age:Int = 0
val address: String by lazy { // <-- ์ฌ๊ธฐ
println("address ์ด๊ธฐํ")
"seoul" //์ด๊ธฐํ๊ฐ ๋ฃ์ด์ค
}
fun displayInfo() {
println("์ด๋ฆ์: ${name} ์
๋๋ค.")
println("๋์ด๋: ${age} ์
๋๋ค.")
println("์ฃผ์๋: ${address} ์
๋๋ค.")
}
}
class LazyTest{
init{
println("init block")
}
val subject by lazy{
println("lazy initialized")
"Kotlin Programming" //๋ฐํ๊ฐ
}
fun flow(){
println("Not initialized")
println("s1 : $subject") //์ต์ด ์ด๊ธฐํ ์์
println("s2 : $subject") //์ด๊ธฐํ๋ ๊ฐ ์ฌ์ฉํ๊ธฐ
}
}
fun main(){
val test = LazyTest()
test.flow()
}
class Person(val name: String, val age : Int)
fun main(){
var isPersonInstantiated: Boolean = false
val person : Person by lazy{
isPersonInstantiated = true
Person("Choi", 22)
}
val personDelegate = lazy { Person("Lee", 40) } //์์๋ณ์๋ฅผ ์ด์ฉํ ์ด๊ธฐํ
println("person Init : $isPersonInstantiated")
println("personDelegate Init: ${personDelegate.isInitialized()}")
println("person.name = ${person.name}") //์ด ์์ ์์ ์ด๊ธฐํ๋จ!
println("personDelegate.value.name = ${personDelegate.value.name}") //์ด ์์ ์์ ์ด๊ธฐํ๋จ
println("person Init: $isPersonInstantiated")
println("personDelegate Init: ${personDelegate.isInitialized()}")
}
//๊ฐ์ฒด์ lazy๊ฐ ์ ์ธ๋ ์์ ์์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ ๊ฒ์ด ์๋๋ผ, ์ฝ๋์ ์ ๊ทผ ์์ ์์ ์ด๊ธฐํ๋จ
//by lazy -> ๊ฐ์ฒด์ ์์
//lazy ํ ๋น -> ๋ณ์์ ์์๋ lazy ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๋ ๊ฒ.
์ด ๋ณ์์ lazy๋ฅผ ํ ๋จ๊ณ ๋ ๊ฑฐ์ณ ๊ฐ์ฒด์ ๋ฉค๋ฒ์ธ value.name๊ณผ ๊ฐ์ ํํ๋ก ์ ๊ทผํด์ผ ํจ
*by๋ฅผ ์ด์ฉํ ์์
1. ํด๋์ค์ ์์
interface Animal{
fun eat(){...}
}
class Cat : Animal{ } //์ธํฐํ์ด์ค
val cat = Cat() //Cat, ์ธํฐํ์ด์ค ๊ตฌํ
class Robot : Animal by cat
Animal์ ์ ์๋ Cat์ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ Robot์ ์์
Cat์ Animal์ private ๋ฉค๋ฒ๋ก Robot ํด๋์ค ์์ ์ ์ฅ๋จ
Cat์์ ๊ตฌํ๋ ๋ชจ๋ Animal์ ๋ฉ์๋๋ ์ ์ ๋ฉ์๋๋ก ์์ฑ๋จ
-> Robot ํด๋์ค์์ Animal์ ๋ช
์์ ์ผ๋ก ์ฐธ์กฐํ์ง ์๊ณ ๋ eat()์ ๋ฐ๋ก ํธ์ถํ ์ ์์
@@275~
'์ธ์ด > Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Kotlin ๋ฌธ๋ฒ ์ข ํฉ] - Single-expression function, ์ฑ๊ธํด (@@์ง๋ฌธํ๊ธฐ) (1) | 2024.03.06 |
|---|---|
| [Kotlin ๋ฌธ๋ฒ ์ข ํฉ] - ๋ ์ธ์ดํํฐ @@์์ ํ๊ธฐ (0) | 2024.03.06 |
| [Kotlin ๋ฌธ๋ฒ ์ข ํฉ] - ์์ธ์ฒ๋ฆฌ # ์์ ํ๊ธฐ (0) | 2024.03.06 |
| [Kotlin ๋ฌธ๋ฒ ์ข ํฉ] - ์ ๊ทผ์ ํ์์ ๊ด๋ จ ์ฉ์ด ์ ๋ฆฌ (0) | 2024.03.06 |
| [Kotlin ๋ฌธ๋ฒ ์ข ํฉ] - ์ธํฐํ์ด์ค, ์ถ์ ํด๋์ค (0) | 2024.03.05 |