当前位置:网站首页>学习 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 这个很酷的特性。让我们在需要时使用这个强大的功能。
边栏推荐
- Kubevera plug-in addons download address
- 第 7 篇:绘制旋转立方体
- Mysql的B+树高度计算
- How many tips do you know about using mock technology to help improve test efficiency?
- 基于BRNN的政务APP评论端到端方面级情感分析方法
- Msfvenom makes master and controlled terminals
- It is said that Microsoft has obtained the supply license for Xianghua! Will Huawei usher in the full lifting of the ban?
- 小程序 canvas 生成海报
- Research on weapon equipment attribute extraction based on attribute word completion
- [Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol
猜你喜欢

Rhcsa first day

Apifox:满足你对 Api 的所有幻想

基于BRNN的政务APP评论端到端方面级情感分析方法

Openeuler embedded sig | distributed soft bus

JVM 内存布局详解(荣耀典藏版)

Log slimming operation: how to optimize from 5g to 1g! (glory Collection Edition)

Gateway technology of IOT technology stack

开放式耳机哪个音质好、公认音质好的气传导耳机推荐

数据插值——对不同量级的数据进行归一化

Object based real-time spatial audio rendering - Dev for dev column
随机推荐
详解visual studio 2015在局域网中远程调试程序
Information fusion method and application of expert opinion and trust in large group emergency decision-making based on complex network
kubevela插件addons下载地址
拥抱开源指南
OA项目之会议通知(查询&是否参会&反馈详情)
Zhuzhou Jiufang middle school carried out drowning prevention and fire safety education and training activities
Construction of Chinese traditional embroidery classification model based on xception TD
基于知识元的外文专利文献知识描述框架
Log slimming operation: how to optimize from 5g to 1g! (glory Collection Edition)
AimBetter洞察您的数据库,DPM 和 APM 解决方案
Data interpolation -- normalize data of different magnitude
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
标准C语言学习总结10
kali里的powersploit、evasion、weevely等工具的杂项记录
凡尔赛天花板:“毕业两年月薪才35K,真是没出息啊~~”
Open earphone which air conduction earphone with good sound quality and recognized sound quality is recommended
第 7 篇:绘制旋转立方体
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
Technology selection rust post analysis
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数