Functional Interface
Java์ @Funtional Interface
์ฒ๋ผ Kotlin์์๋ ํ๋์ ์ถ์ ๋ฉ์๋๋ง ๊ฐ์ง ์ ์๊ฒ ๊ฐ์ ํ ์์์ต๋๋ค.
fun interface SampleInterface {
fun index(value:Int): String
}
default ๋ฉ์๋๋ ์ ์ธ๋ฉ๋๋ค.
fun interface SampleInterface {
fun index(value:Int): String
fun index(value:String): String = "hello $value"
}
์ฌ์ฉ
SAM์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ ์๋๋ฐฉ๋ฒ์ด ์กด์ฌํฉ๋๋ค โป SAM : ๋๋ค์์ ์ด์ฉํ ๋ณํ
fun sample(value: Int) : String = object : SampleInterface {
override fun index(value: Int): String {
return "hello $value"
}
}.index(value)
fun sample(value: Int) : String = SampleInterface { "hello $it" }.index(value)
Last updated