728x90
package com.example.kotlin
fun main() {
//확장함수 Extension function
// 기존에 정의되어 있는 클래스에 함수를 추가하는 기능
Test().hello()
val test = Test()
test.hi()
}
fun Test.hi() = println("하이")
class Test() {
fun hello() = println("안녕")
fun bye() = println("잘가")
}
확장함수 기능 덕분에 상속을 해서 함수를 추가하지 않아도 되는 등
편리하다
728x90
'코틀린(Kotlin)' 카테고리의 다른 글
| (코틀린/Kotlin) 초기화 지연 (0) | 2024.02.08 |
|---|---|
| (코틀린/Kotiln) Scope function (0) | 2024.02.08 |
| (코틀린/Kotlin) Lambda(람다) (0) | 2024.02.08 |
| (코틀린/Kotlin) String template (0) | 2024.02.06 |
| (코틀린/Kotlin) 타입체크&캐스팅 (0) | 2024.02.06 |