κΉ¨μ•Œ κ°œλ… πŸ“‘/Kotlin

[Kotlin] ν™•μž₯ ν•¨μˆ˜(Extension functions)

interfacer_han 2023. 12. 16. 18:09

#1 μ½”λ“œ - μ½”ν‹€λ¦°

#1-1

fun main() {
    var testInteger : Int = 7
    testInteger = testInteger.plusAndMultiply(3, 5)
    println("${testInteger}") // 결과둜 50이 좜λ ₯λœλ‹€.
}

fun Int.plusAndMultiply(plusValue : Int, multiplyValue : Int) : Int {
    return (this + plusValue) * multiplyValue
}

이 μ½”λ“œλŠ” ν™•μž₯ ν•¨μˆ˜(Extension functions)λ₯Ό μ‚¬μš©ν•˜λŠ” μ˜ˆμ΄λ‹€.

확μž₯ ν•¨μˆ˜λž€ κΈ°μ‘΄ 클래슀의 멀버 ν•¨μˆ˜μΈ κ²ƒμ²˜λŸΌ ν˜ΈμΆœν•  수 μžˆμ§€λ§Œ, ν•΄λ‹Ή ν΄λž˜μŠ€μ— 직접 μ •μ˜λœ 멀버가 μ•„λ‹Œ μ™ΈλΆ€μ—μ„œ μ •μ˜λœ ν•¨μˆ˜λ‹€. ν™•μž₯ ν•¨μˆ˜μ˜ 문법은 μ•„μ£Ό κ°„λ‹¨ν•˜λ‹€. ν•¨μˆ˜ 이름 μ•žμ— 클래슀 λ˜λŠ” μΈν„°νŽ˜μ΄μŠ€ λͺ…을 λΆ™μ΄κΈ°λ§Œ ν•˜λ©΄ λœλ‹€.β€¨μ—¬λ‹΄μœΌλ‘œ, μžλ°”μ—λŠ” μ½”ν‹€λ¦°μ˜ ν™•μž₯ ν•¨μˆ˜μ™€ 같은 κΈ°λŠ₯은 μ—†μ§€λ§Œ λΉ„μŠ·ν•œ 역할을 μˆ˜ν–‰ν•˜λŠ” Default methodκ°€ μžˆλ‹€.

 

#1-2

fun main() {
    val hongGilDong = Person("홍길동", 175, 65, 3579)
    Person.hello()
    hongGilDong.selfIntroduction()
    hongGilDong.introduceBMI()
    Person.bye()
}

class Person (val name: String, val height: Int, val weight: Int, private val memberNumber : Int) {

    companion object {
        fun hello() {
            println("Hello")
        }
        fun bye() {
            println("GoodBye")
        }
    }
    
    fun selfIntroduction() {
        println("My name is ${name}. I am ${height}cm tall and weigh ${weight}kg")
    }
}

fun Person.introduceBMI() {
    val weightKG = this.weight.toDouble()
    val heightM = this.height.toDouble() / 100
    val BMI = weightKG / (heightM * heightM) // heightM * heightM에 κ΄„ν˜Έλ₯Ό μ”Œμš°μ§€ μ•ŠμœΌλ©΄ BMI == weightKGκ°€ λ˜μ–΄λ²„λ¦¬λ‹ˆ 주의
    val roundedBMI = "%.1f".format(BMI)
    println("My BMI is ${roundedBMI}")
}

/* ↓ μ—λŸ¬κ°€ λ°œμƒν•˜λŠ” μ½”λ“œ
fun Person.introduceMemberNumber() {
    println("My memberNumber is ${this.memberNumber}")
}
*/

/* 좜λ ₯ κ²°κ³Ό
Hello
My name is 홍길동. I am 175cm tall and weigh 65kg
My BMI is 21.2
GoodBye
*/

ν™•μž₯ ν•¨μˆ˜λŠ” κΈ°μ‘΄ 클래슀의 멀버 ν•¨μˆ˜μΈ κ²ƒμ²˜λŸΌ ν˜ΈμΆœν•  수 μžˆμ„ 뿐, μ‹€μ œλ‘œ ν•΄λ‹Ή 클래슀의 멀버인 것은 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— privateμ΄λ‚˜ protected λ©€λ²„μ—λŠ” μ ‘κ·Όν•  수 μ—†λ‹€. 예λ₯Ό λ“€μ–΄, μœ„ μ½”λ“œμ—μ„œ Person 객체의 memerNumber ν”„λ‘œνΌν‹°μ—λŠ” ν™•μž₯ ν•¨μˆ˜κ°€ μ ‘κ·Όν•  수 μ—†λ‹€.

 

#1-3

fun main() {
    val hongGilDong = Person("홍길동", 175, 65, 3579)
    
    hongGilDong.introduceType()
}

interface Animal {
    val type : String
}

class Person (val name: String, val height: Int, val weight: Int, private val memberNumber : Int) : Animal {
    override val type = "mammal"
    
    // ...
}

fun Animal.introduceType() {
    println("My type is ${this.type}")
}

/* 좜λ ₯ κ²°κ³Ό
My type is mammal
*/

이과 같이 μΈν„°νŽ˜μ΄μŠ€μ—λ„ ν™•μž₯ ν•¨μˆ˜λ₯Ό μ μš©ν•  수 μžˆλ‹€.

 

#2 μš”μ•½

ν™•μž₯ ν•¨μˆ˜λ₯Ό 톡해, μ›λž˜μ˜ μ½”λ“œλ₯Ό κ±΄λ“œλ¦¬μ§€ μ•Šκ³  κΈ°λŠ₯을 μΆ”κ°€ν•œλ‹€.

 

#3 이 κ°œλ…μ΄ μ‚¬μš©λœ κΈ€

 

[λ°±μ€€] 10845 - 큐

#1 μ•Œκ³ λ¦¬μ¦˜ java.util.Stackκ³ΌλŠ” 달리 java.util.QueueλŠ” ν΄λž˜μŠ€κ°€ μ•„λ‹ˆλΌ μΈν„°νŽ˜μ΄μŠ€λ‹€. λ‚˜λŠ” java.util.Queue μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ 클래슀인 LinkedListλ₯Ό Queueλ‘œμ„œ μ‚¬μš©ν–ˆλ‹€. last()λŠ” ν™•μž₯ ν•¨μˆ˜(Extension functions)

kenel.tistory.com

 

[λ°±μ€€] 2606 - λ°”μ΄λŸ¬μŠ€

#1 μ•Œκ³ λ¦¬μ¦˜ #2 μ½”λ“œ (μ½”ν‹€λ¦°) fun main() { readln() // μ»΄ν“¨ν„°μ˜ κ°―μˆ˜λŠ” 풀이에 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€. val connectionCount = readln().toInt() val connections : ArrayList = ArrayList() for(i : Int in 0..

kenel.tistory.com