当前位置:网站首页>Interesting kotlin 0x09:extensions are resolved statically
Interesting kotlin 0x09:extensions are resolved statically
2022-07-28 16:45:00 【AndroidKt】
Recently http://portal.kotlin-academy.com/#/ I see a lot about Kotlin An interesting topic . I think it's very suitable for Kotlin lovers , If you are interested, you can check it by yourself .
【 amusing Kotlin 】 Record your understanding of each question in the series .
0x09:Extensions are resolved statically
open class C
class D: C()
fun C.foo() = "c"
fun D.foo() = "d"
fun printFoo(c: C) {
println(c.foo())
}
fun main(args: Array<String>) {
printFoo(D())
}
Above code , What is the result of the operation ? optional :
- Doesn’t Compile
- Rutime error
- "c"
- "d"
Think about it , Record the answers in your mind .
analysis
Basic information

class
Cyes classDParent class of , And each has a namefooThe extension function of . classCThe extension function of returns a string “c” , classDThe extension function of returns a string “d”;Defines a top-level function
printFoo(), Parameter type isC;mainCall inside functionprintFoofunction , And the passed in parameter isDExample .
Habitual thinking
main() Function printFoo() The incoming parameter is D object , Nature calls D The extension function of D.foo() , So the answer to this question is —— Options 4:“d”.
Is this analysis right ?
If Kotlin The essence of extension function is member function , There is no problem with this analysis , But is the essence of extension function a member function ?
Kotlin What is the essence of extension function ?
If something is unknown, give an example
Define an extension function ,receiver by String.
fun String.suffix(suffix: String) = this + suffix
Decompile if something is unknown
- Bytecode

- Decompile Java Code

- Kotlin spread function stay Java Use
public class Test {
public static void main(String[] args) {
// Two parameters and class member function
String result =
Extensions_are_resolved_staticallyKt.suffix("Hello", " OpenCV or Android");
System.out.println(result);
}
}
Sum up , Kotlin The essence of extension function is static function , And receiver Is the first parameter of the corresponding static function in the bytecode . therefore Java Call in Kotlin When defining an extension function , Need to receiver As the first parameter .
Problem solving
Back to topic
fun printFoo(c: C) {
println(c.foo())
}
Because the parameter type is C, So in the code c.foo() The corresponding is fun C.foo() = "c" Compiled into static functions . Although the runtime passes in D(), But it will still be forced to C And then execute fun C.foo() = "c" The corresponding static method . therefore , In question , The correct answer is :
Options 3 :"c"

extend
We give classes C and class D Add member functions respectively foo()
package puzzlers
open class C {
open fun foo() = "cc" // class C Member functions foo
}
class D : C() {
override fun foo() = "dd" // class D Member functions foo
}
fun C.foo() = "c"
fun D.foo() = "d"
fun printFoo(c: C) {
println(c.foo())
}
fun main(args: Array<String>) {
printFoo(D())
}
What is the result of the operation ? Friendship tips member always win.
边栏推荐
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
- 栈的介绍与实现(详解)
- HM secondary development - data names and its use
- Headline article_ signature
- 排序4-堆排序与海量TopK问题
- 小程序:scroll-view默认滑倒最下面
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- Applet: get element node information
猜你喜欢

IM即时通讯开发优化提升连接成功率、速度等

Introduction and implementation of stack (detailed explanation)

laravel

快速掌握 Kotlin 集合函数

Sort 4-heap sort and massive TOPK problem

WSL+Valgrind+Clion

Debugging methods of USB products (fx3, ccg3pa)

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

Applet: get element node information

Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
随机推荐
About mit6.828_ HW9_ Some problems of barriers xv6 homework9
egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
Pop up layer prompt in the background
Microsoft question 100 - do it every day - question 11
PHP mb_ Substr Chinese garbled code
有趣的 Kotlin 0x06:List minus list
在abaqus中使用PyQt设计GUI
Introduction and implementation of stack (detailed explanation)
asp.net大文件分块上传断点续传demo
PHP calculate coordinate distance
LeetCode-学会复杂带随机指针链表的题(详解)
微软100题-天天做-第11题
局域网无法访问apache服务器
2021-04-02
Geodetic coordinate system to Martian coordinate system
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
LwIP develops | socket | TCP | keepalive heartbeat mechanism
Hdu1847 problem solving ideas
一小时内学会Abaqus脚本编程秘籍