当前位置:网站首页>学习 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 这个很酷的特性。让我们在需要时使用这个强大的功能。
边栏推荐
- ESP8266-Arduino编程实例-定时器与中断
- 使用百度EasyDL实现明厨亮灶厨师帽识别
- Matlab from introduction to mastery Chapter 1 Introduction to matlab
- Mesh data generation function meshgrid
- Open earphone which air conduction earphone with good sound quality and recognized sound quality is recommended
- Research on weapon equipment attribute extraction based on attribute word completion
- 小程序 组件 定时器的清除
- Layout the 6G track in advance! Ziguang zhanrui released the white paper "6G unbounded AI"
- Introduction to C language [detailed]
- For the 1000 yuan 5g mobile phone market, MediaTek Tianji 700 released
猜你喜欢

基于复杂网络的大群体应急决策专家意见与信任信息融合方法及应用

How to search images efficiently and accurately? Look at the lightweight visual pre training model

Gateway technology of IOT technology stack

怎样巧用断言+异常处理类,使代码更简洁!(荣耀典藏版)

纳米金偶联抗体/蛋白试剂盒(20nm,1mg/100μg/500 μg偶联量)的制备

开放式耳机哪个品牌好、性价比最高的开放式耳机排名

比UUID更快更安全NanoID到底是怎么实现的?(荣耀典藏版)

Construction of Chinese traditional embroidery classification model based on xception TD

Mesh data generation function meshgrid

大学荒废三年,大四自学7个月测试,找到了12K的工作
随机推荐
融合LSTM与逻辑回归的中文专利关键词抽取
微信小程序开发公司你懂得选择吗?
Make trouble fishing day by day
使用Mock技术帮助提升测试效率的小tips,你知道几个?
HCIA综合实验(以华为eNSP为例)
Pytoch learning record (III): random gradient descent, neural network and full connection
Nano gold coupled antibody / protein Kit (20nm, 1mg/100 μ g/500 μ G coupling amount) preparation
How is nanoid faster and more secure than UUID implemented? (glory Collection Edition)
Leetcode 142. circular linked list II [knowledge points: speed pointer, hash table]
JVM 内存布局详解(荣耀典藏版)
日志瘦身神操作:从5G优化到1G到底是怎么做到的!(荣耀典藏版)
Esp8266 Arduino programming example - deep sleep and wake up
Use pl/sql
Esp8266 Arduino programming example - SPIFs and data upload (Arduino IDE and platformio IDE)
RHCSA第一天
kingbase中指定用户默认查找schema,或曰用户无法使用public schema下函数问题
Byte side: can TCP and UDP use the same port?
Cy3/cy5/cy5.5/cy7 fluorescent labeling antibody / protein Kit (10~100mg labeling amount)
管理区解耦架构见过吗?能帮客户搞定大难题的
Introduction to wechat applet development, develop your own applet