当前位置:网站首页>学习 Kotlin - 扩展函数
学习 Kotlin - 扩展函数
2022-07-28 20:21:00 【小陈乱敲代码】
扩展功能
什么是扩展功能?
顾名思义,扩展函数是帮助我们扩展类的功能而无需接触它们的代码的函数。
换句话说,Kotlin 中的扩展函数允许我们通过添加新函数来扩展类的功能。该类不必属于我们(可以是第三方库),也不需要我们继承该类。
举个很简单的例子来理解。
fun Int.triple(): Int {
return this * 3
}
// now we can use like this
var result = 3.triple()
另一个例子,让我们看看如何在 Android View 中使用它。
fun ImageView.loadImage(url: String) {
Glide.with(context).load(url).into(this)
}
// now we can use like this
imageView.loadImage(url)
这看起来很棒!
在 Android 开发中有很多地方,我们可以使用 Kotlin 这个很酷的特性。让我们在需要时使用这个强大的功能。
边栏推荐
- 39. Combined sum
- Object based real-time spatial audio rendering - Dev for dev column
- 日志瘦身神操作:从5G优化到1G到底是怎么做到的!(荣耀典藏版)
- System Analyst
- Oracle built-in functions
- Research on the recognition method of move function information of scientific paper abstract based on paragraph Bert CRF
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- Matlab | basic knowledge summary I
- What technology is needed for applet development
- PCB材料简单介绍
猜你喜欢
磷脂偶联抗体/蛋白试剂盒的存储与步骤
Leetcode · 581. shortest unordered continuous subarray · double pointer
微信小程序开发入门,自己开发小程序
Rhcsa first day
Gateway technology of IOT technology stack
Lt7911d type-c/dp to Mipi scheme is mature and can provide technical support
Pytoch learning record (III): random gradient descent, neural network and full connection
使用Mock技术帮助提升测试效率的小tips,你知道几个?
中文招聘文档中专业技能词抽取的跨域迁移学习
详解visual studio 2015在局域网中远程调试程序
随机推荐
使用Mock技术帮助提升测试效率的小tips,你知道几个?
Getting started with Oracle
Knowledge description framework of foreign patent documents based on knowledge elements
节省70%的显存,训练速度提高2倍!浙大&阿里提出在线卷积重新参数化OREPA,代码已开源!(CVPR 2022 )
II. Explanation of the sequence and deserialization mechanism of redistemplate
ESP8266-Arduino编程实例-SPIFFS及数据上传(Arduino IDE和PlatformIO IDE)
B+ tree height calculation of MySQL
Lt7911d type-c/dp to Mipi scheme is mature and can provide technical support
Official document of kubevela 1.4.x
JVM 内存布局详解(荣耀典藏版)
第 7 篇:绘制旋转立方体
Which brand is the best and most cost-effective open headset
基于BRNN的政务APP评论端到端方面级情感分析方法
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
HCIA综合实验(以华为eNSP为例)
系统分析师
怎样巧用断言+异常处理类,使代码更简洁!(荣耀典藏版)
Professional Committee of agricultural water and soil engineering of China Association of Agricultural Engineering - 12th session - Notes
Object.prototype.toString.call()的原理
Matlab|基础知识总结一