확장 함수 - swkim0128/PARA GitHub Wiki
- 확장 함수는 기존 클래스의 멤버 함수처럼 호출할 수 있는 함수를 외부에서 추가하는 기능입니다.
- 코틀린의 주요 특징 중 하나로, **오픈/클로즈 원칙(OCP)**을 준수하며 클래스의 동작을 확장할 수 있습니다.
기본 문법
fun 클래스이름.함수이름(매개변수): 반환타입 {
// 함수 본문
}
예제
fun String.addExclamation(): String {
return this + "!"
}
fun main() {
val message = "Hello"
println(message.addExclamation()) // 출력: Hello!
}
특징:
- this 키워드: 확장 함수 내부에서 호출되는 객체를 참조합니다.
- 원본 클래스: 클래스의 원래 정의를 변경하지 않습니다.
fun List<Int>.sum(): Int {
return this.reduce { acc, i -> acc + i }
}
fun main() {
val numbers = listOf(1, 2, 3, 4)
println(numbers.sum()) // 출력: 10
}
fun Int.isEven(): Boolean {
return this % 2 == 0
}
fun main() {
val number = 4
println(number.isEven()) // 출력: true
}
fun String.removeSpaces(): String {
return this.replace(" ", "")
}
fun main() {
val text = "Hello Kotlin"
println(text.removeSpaces()) // 출력: HelloKotlin
}
- 클래스에 정의된 멤버 함수와 동일한 이름의 확장 함수가 존재할 경우, 멤버 함수가 우선됩니다.
예제
class Example {
fun printMessage() {
println("멤버 함수 호출")
}
}
fun Example.printMessage() {
println("확장 함수 호출")
}
fun main() {
val example = Example()
example.printMessage() // 출력: 멤버 함수 호출
}
- 확장 프로퍼티를 사용하여 기존 클래스에 속성을 추가할 수 있습니다.
- **Backing Field(필드)**는 가질 수 없으며, 반드시 Getter를 제공해야 합니다.
문법
val 클래스이름.프로퍼티이름: 타입
get() = 값
예제
val String.wordCount: Int
get() = this.split(" ").size
fun main() {
val message = "Kotlin is awesome"
println(message.wordCount) // 출력: 3
}
- 확장 함수는 클래스의 멤버가 아니므로 오버라이드할 수 없습니다.
예제
open class Parent
class Child : Parent()
fun Parent.greet() = "Hello from Parent"
fun Child.greet() = "Hello from Child"
fun main() {
val parent: Parent = Child()
println(parent.greet()) // 출력: Hello from Parent
}
- 확장 함수는 클래스의 private 또는 protected 멤버에 접근할 수 없습니다.
예제
class Example {
private val secret = "비밀"
}
fun Example.revealSecret(): String {
// return secret // 오류: private 멤버에 접근할 수 없음
return "접근 불가"
}
예제
open class Animal
class Dog : Animal()
fun Animal.sound() = "동물이 소리를 냅니다."
fun Dog.sound() = "멍멍!"
fun main() {
val animal: Animal = Dog()
println(animal.sound()) // 출력: 동물이 소리를 냅니다.
}
이유:
- 확장 함수는 정적으로 결정되며, 실제 객체 타입이 아닌 참조 타입에 따라 결정됩니다.
fun String.isPalindrome(): Boolean {
return this == this.reversed()
}
fun main() {
println("radar".isPalindrome()) // 출력: true
println("hello".isPalindrome()) // 출력: false
}
fun Any.log() {
println("[LOG] $this")
}
fun main() {
val message = "Hello, Kotlin!"
message.log() // 출력: [LOG] Hello, Kotlin!
}
개념 | 설명 |
---|---|
확장 함수 | 기존 클래스에 새로운 함수를 추가하는 기능 |
확장 프로퍼티 | Getter를 이용해 클래스에 새로운 속성을 추가 |
제한 사항 | 멤버 함수보다 우선순위가 낮으며, private/protected 멤버에 접근 불가 |
활용 사례 | 데이터 처리, 유틸리티 함수, 가독성 향상 등 다양한 용도로 사용 가능 |
확장 함수는 코틀린의 강력한 기능 중 하나로, 기존 클래스의 동작을 변경하지 않고도 동작을 확장할 수 있습니다. 이를 활용하면 코드의 가독성과 재사용성을 크게 높일 수 있습니다.