#1 μΌλ° μΈν°νμ΄μ€
#1-1 νλ²ν μΈν°νμ΄μ€
interface MyNormalInterface {
fun myFirstMethod(value: Int): String
fun mySecondMethod(value1: Int, value2: String): Int
...
}
μ°λ¦¬κ° μ μκ³ μλ μΈν°νμ΄μ€μ λͺ¨μ΅μ΄λ€.
#1-2 ν¨μν μΈν°νμ΄μ€μ ν보
// ν¨μν μΈν°νμ΄μ€μ ννμ§λ§, ν¨μν μΈν°νμ΄μ€λ μλ
interface MyFunctionalInterfaceCandidate {
fun myMethod(value1: Int, value2: Int): Int
}
κ°μ§ λ©μλκ° μΆμ λ©μλ νλλΏμΈ μΈν°νμ΄μ€(Single Abstract Method, SAM)λ₯Ό νΉλ³ν ν¨μν μΈν°νμ΄μ€λΌκ³ λΆλ₯Έλ€. ν΄λΉ μΈν°νμ΄μ€μμ μ¬μ©ν ν¨μκ° κ°μ§κ³ μλ νλμ ν¨μλ‘ λ°μ κ²°μ λμ§ μμΌλ, MyFunctionalInterface μ체λ₯Ό myMethod()λΌκ³ μκ°νλ€. κ·Έλμ 'ν¨μν μΈν°νμ΄μ€'λΌλ μ΄λ¦μ΄ λΆμλ€.
νμ§λ§, μ΄ λ Όλ¦¬λΌλ©΄ κ°μ§κ³ μλ νλλΏμΈ λ©μλκ° μΆμ λ©μλκ° μλμ΄λ 'ν¨μν μΈν°νμ΄μ€'λΌκ³ λΆλ₯Ό μλ μλ κ² μλλλ μλ¬Έμ΄ μκΈ΄λ€. νμ§λ§, κ·Έ κ²½μ°μ κ·Έλ₯ κ·Έ ν¨μλ₯Ό λ°λ‘ νΈμΆνλ©΄ κ·Έλ§μ΄λ€. λ°λΌμ κ΅³μ΄ ν¨μν μΈν°νμ΄μ€λΌλ κ°λ μ λ§λ€ νμμ‘°μ°¨ μλ€. κ²°λ‘ μ μΌλ‘, ν¨μν μΈν°νμ΄μ€λ ꡬν κ°λ₯ν ν¨μκ° νλ λΏμΈ μΈν°νμ΄μ€λ₯Ό κ°λ¦¬ν€λ μ©μ΄λ€.
νλ μ μ½λλ ν¨μν μΈν°νμ΄μ€κ° μλλ€. ν¨μν μΈν°νμ΄μ€κ° λ μ μλ νλ³΄μΌ λΏμ΄λ€. μ½νλ¦°μμ ν¨μν μΈν°νμ΄μ€λ₯Ό λ§λλ €λ©΄ SAM Conversionsλ₯Ό ν΄μΌ νλ€. μλ μ½λλ₯Ό 보μ.
#2 ν¨μν μΈν°νμ΄μ€ (Functional Interface, Single Abstract Method Interface, SAM)
fun interface MyFunctionalInterface {
fun myMethod(value1: Int, value2: Int): Int
}
interface ν€μλ μμ funμ λΆμλ€. μ΄λ μ½νλ¦° μ»΄νμΌλ¬μκ² μ΄ μΈν°νμ΄μ€κ° ν¨μν μΈν°νμ΄μ€μμ λͺ μμ μΌλ‘ μ리λ νλμ΄λ€. μ΄ κ°λ¨ν νλμ SAM ConversionsλΌκ³ νλ€. λ§ κ·Έλλ‘ ν¨μν(fun) μΈν°νμ΄μ€(interface)κ° λ κ²μ΄λ€. μ΄μ λΆν° λΉλ‘μ Kotlinμ ν΄λΉ μΈν°νμ΄μ€λ₯Ό ν¨μν μΈν°νμ΄μ€λ‘ μΈμνλ€. (λΉμ°νκ² μ§λ§, fun interfaceμ μΆμ λ©μλλ₯Ό 2κ° μ΄μ λ£μΌλ©΄ μλ¬κ° λλ€.)
Javaμμ ν¨μν μΈν°νμ΄μ€λ₯Ό μ»΄νμΌλ¬κ° μλμΌλ‘ μΈμνλ€. μ¦, μ½νλ¦°μμμ²λΌ fun ν€μλλ₯Ό λΆμ΄κ±°λ νλ μμ μ΄ μμλ€. λ°λ©΄, Javaλ₯Ό κ³μΉν Kotlinμμ μΌλ° μΈν°νμ΄μ€μ ν¨μν μΈν°νμ΄μ€λ₯Ό λͺ μμ μΌλ‘ ꡬλΆμ§λλ€. κ·Έ ꡬλΆμ ν΅ν΄ νλ‘κ·Έλλ¨Έμκ² λͺ μμ±κ³Ό μμ μ±μ μ 곡νκΈ° μν¨μ΄λ€.
#3 μ΅λͺ ν΄λμ€μ μΈμ€ν΄μ€ λ§λ€κΈ°
#3-1 μΌλ° μΈν°νμ΄μ€
interface MyNormalInterface {
fun myFirstMethod(value: Int): String
fun mySecondMethod(value1: Int, value2: String): Int
}
fun main() {
val instance = object : MyNormalInterface {
override fun myFirstMethod(value: Int): String {
return "μ무λ§"
}
override fun mySecondMethod(value1: Int, value2: String): Int {
println("value2λ $value2")
return value1 + 1
}
}
}
μμ©κ΅¬ μ½λ(Boilerplate code)κ° λ§μ 볡μ‘ν΄ λ³΄μΈλ€.
#3-2 ν¨μν μΈν°νμ΄μ€ (Functional Interface, Single Abstract Method Interface)
fun interface MyFunctionalInterface {
fun myMethod(value1: Int, value2: Int): Int
}
fun main() {
val instance = object : MyFunctionalInterface {
override fun myMethod(value1: Int, value2: Int): Int {
return value1 * value2 + 1
}
}
}
μ΄μ²λΌ ν¨μν μΈν°νμ΄μ€ λν μΌλ° μΈν°νμ΄μ€μμμ κ°μ΄ μμ©κ΅¬ μ½λλ‘ λ²λ² (?)λ μ½λλ₯Ό 지 μ μλ€. νμ§λ§, ν¨μν μΈν°νμ΄μ€λ μ νμ§κ° νλ λ μλ€.
fun interface MyFunctionalInterface {
fun myMethod(value1: Int, value2: Int): Int
}
fun main() {
val instance = MyFunctionalInterface { value1, value2 -> value1 * value2 + 1 }
}
ν¨μν μΈν°νμ΄μ€ μ΄λ¦ + λλ€ ννμ μ‘°ν©μ΄λ©΄ λ°λ‘ μ΅λͺ ν΄λμ€ μμ±κ³Ό μΈμ€ν΄μ€νκ° λμμ, κ°νΈνκ² μ΄λ€μ§λ€. μ΄μ°¨νΌ ν΄λΉ μΈν°νμ΄μ€κ° κ°μ§ ꡬν κ°λ₯ν λ©μλλ λ¨ ν κ°λΏμ΄λΌλ λΆλͺ ν μ¬μ€μ΄ μκΈ°μ κ°λ₯ν κ³Όκ°ν λ¬Έλ²μ΄λ€.
#4 μμ½
ν¨μν μΈν°νμ΄μ€κ° λ¨ ν κ°μ λ©μλλ₯Ό κ°μ§λ€λ μ¬μ€μ μ΄μ©ν΄, μμ©κ΅¬ μ½λλ₯Ό μ κ±°νλ€.
'κΉ¨μ κ°λ π > Kotlin' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Kotlin] λλ€(Lambda) ννμ (0) | 2024.02.01 |
---|---|
[Kotlin] ν¨μ νμ (Fuction types) ννμ (0) | 2024.01.31 |
[Kotlin] μ°μ°μ μ€λ²λ‘λ© (Operator overloading) (0) | 2024.01.29 |
[Kotlin] νλ‘νΌν°(Property) (0) | 2024.01.17 |
[Kotlin] νμ₯ ν¨μ(Extension functions) (0) | 2023.12.16 |