当前位置:网站首页>Kotlin的扩展函数知识点
Kotlin的扩展函数知识点
2022-04-22 15:09:00 【InfoQ】
为什么需要扩展
扩展函数是什么
fun MutableList<Int>.exchange(fromIndex:Int, toIndex:Int) {
val tmp = this[fromIndex]
this[fromIndex] = this[toIndex]
this[toIndex] = tmp
}
扩展函数怎么用
val list = mutableListOf(1,3,5)
list.exchange(1,5)
再谈扩展函数是什么
public static final void exchange(@NotNull List $receiver, int fromIndex, int toIndex) {
//检查$receiver参数是否为空。receiver就是调用者
Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
int tmp = ((Number)$receiver.get(fromIndex)).intValue();
$receiver.set(fromIndex, $receiver.get(toIndex));
$receiver.set(toIndex, Integer.valueOf(tmp));
}
扩展函数在哪里可以被使用
- 不定义在类中,也就是类外部可以看到上面反编译后的扩展函数就是这种类型,被static,public,final修饰的方法会有这个特征:在同一个包中是可以共享这个扩展函数的也就是可以调用到这个扩展函数。其他包里面如果也想使用这个函数就可以import这个包中的这个函数即可。
- 定义在类中,也就是类内部这时候诡异的事情出现了,扩展函数无法被调用。接下来看下对应的扩展函数反编译后的字节码:
public final void exchange(@NotNull List $receiver, int fromIndex, int toIndex) {
Intrinsics.checkParameterIsNotNull($receiver, "$receiver");
int tmp = ((Number)$receiver.get(fromIndex)).intValue();
$receiver.set(fromIndex, $receiver.get(toIndex));
$receiver.set(toIndex, Integer.valueOf(tmp));
}
扩展函数的限制
静态扩展函数
class Son {
companion object {
//该变量为静态变量
val age = 10
}
}
fun Son.Companion.foo() {
println("age = $age")
}
函数优先级
this的指向
扩展函数注意点
- 1.如果该扩展函数定义在类内部就是顶级函数/成员函数,不能被覆盖;(因为是基于运行时类型)
- 2.我们无法访问其接收器的非公共属性;(本质是将其变为方法的第一个参数)
- 3.扩展接收器总是被静态调度。(和重载一样)
- 4.也是最重要的一点,不要滥用扩展特性,思考好合适的接受者receivers,不要什么都往context上堆;参数简化要考虑是否有副作用
总结
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/d8bdde1a3ef4ac887dd43bc9d
边栏推荐
- Pondering method reference and lambda -- vernacular lambda and method reference
- 【深入理解TcaplusDB技术】读取列表所有数据示例代码——[List表]
- 爱站网关键词挖掘查询工具-批量网站关键词挖掘导出软件免费下载
- Operation of simulated examination platform for examination questions of safety production management personnel of hazardous chemical production units in 2022
- Introduction to openvino
- 【深入理解TcaplusDB技术】更新数据示例代码——[Generic表]
- 【深入理解TcaplusDB技术】读取数据示例代码——[Generic表]
- Introduction to JVM garbage collection serial, serial old, parallel scavenge, parallel old and STW (stop the world)
- 【深入理解TcaplusDB技术】批量删除列表指定位置数据接口说明——[List表]
- 人脸识别 (5) 基于MCTNN人脸检测(Pytorch)
猜你喜欢

Introduction to openvino

wxWidgets学习笔记(一):解读Code::Blocks模板工程源代码

【深入理解TcaplusDB技术】示例代码——数据分批返回

如何通过云效Projex项目协同提高团队更高效的协作能力

There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server

如何维护和应用高压隔离探头

小心间谍出没!维护国家安全,你我共同守护

Memcpy() function copies two-dimensional array & memcmp() function compares two-dimensional array

2022年化工自动化控制仪表考试题库及在线模拟考试

*CTF2022 - Web
随机推荐
带你了解极具弹性的Spark架构的原理
share memory的bank conflict分析
【深入理解TcaplusDB技术】扫描数据示例代码——[Generic表]
Alibaba cloud IOT transfer to PostgreSQL database scheme
wxWidgets学习笔记(一):解读Code::Blocks模板工程源代码
Android的UI---ZoomControls放大缩小图片,android面试简历模板
如何维护和应用高压隔离探头
How to import Cisco evng image simulator
Redis面试题汇总,mysql索引优化面试题常问
干货!对有目标对抗图像迁移性的反思
When there is only input and output in flinksql, it will be merged into a subtask. In this case, the amount of input and output data cannot be displayed?
百合医疗IPO被终止:实控人黄凯之父黄维郭曾是佛山副市长
爱站网关键词挖掘查询工具-批量网站关键词挖掘导出软件免费下载
[Mysql] CHAR_LENGTH函数
Configure MySQL Cluster with MYCAT (2) -- configure MySQL master-slave replication
Shortcut key delete browser cache, windows combination key, CMD shortcut command
2022年C#发展趋势
About STS access control of Alibaba cloud OSS resources
不用真人露脸拍视频,方法在这里,做自媒体20天4561
dried food! Reflection on the image migration of targeted confrontation