当前位置:网站首页>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.
边栏推荐
- Signal and slot mechanism of QT learning
- IM即时通讯开发优化提升连接成功率、速度等
- mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
- Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
- About standard IO buffers
- 栈的介绍与实现(详解)
- ANSYS二次开发 - MFC界面调用ADPL文件
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
- ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
- 队列的介绍与实现(详解)
猜你喜欢

ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)

About the web docking pin printer, lodop uses

Learn ABAQUS script programming script in an hour

The epidemic dividend disappeared, and the "home fitness" foam dissipated

每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...

Ansa secondary development - apps and ansa plug-in management
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued

QT packaging
随机推荐
Installation of QT learning
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
Some suggestions on optimizing HyperMesh script performance
微软100题-天天做-第11题
Analysis of echo service model in the first six chapters of unp
Reset grafana login password to default password
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
Learn ABAQUS script programming script in an hour
ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
Li Hongyi, machine learning 4. Deep learning
The epidemic dividend disappeared, and the "home fitness" foam dissipated
Detailed record of steps to configure web server (many references)
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
优化Hypermesh脚本性能的几点建议
局域网无法访问apache服务器
The video Number finds the golden key, and Tiktok imitates the latecomers
Li Hongyi, machine learning 5. Tips for neural network design