How are extensions resolved in Kotlin and what doest it mean?
How are extensions resolved in Kotlin and what doest it mean?
Extensions in Kotlin are resolved statically, which means that the extension function to be called is determined at compile time based on the declared type of the variable, not its runtime type.
For example, if you have:
open class Shape
class Rectangle : Shape()
fun Shape.getName() = "Shape"
fun Rectangle.getName() = "Rectangle"
fun printClassName(s: Shape) {
println(s.getName())
}
And call printClassName(Rectangle()), it will print "Shape", because the extension function called depends only on the declared type of the parameter s, which is Shape.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.