当前位置:网站首页>Function extension, attribute extension and non empty type extension in kotlin
Function extension, attribute extension and non empty type extension in kotlin
2022-07-07 08:14:00 【yu-Knight】
val myStr: String = "AAA"
// Extended attributes
val String.myInfo: String
get() = "yuknight"
fun String.showPrintln(): String {
println(" Printout , Parallel chain call , The content is :$this")
return this
}
val String.strValue: String
get() = " At present ${System.currentTimeMillis()}, Call once , The value is :$this, The length is $length"
// Yes String?== Function extension of null type
fun String?.outputStrValueF(defaultValue:String) = println(this ?: defaultValue)
fun String?.outputStrValueFget(defaultValue: String) = if (this == null) defaultValue else this
fun main(){
val str:String = "ABC"
println(str.myInfo)
// spread function
str.showPrintln().showPrintln().showPrintln()
// Extended attributes
str.myInfo.showPrintln().showPrintln()
"yuknight".strValue.showPrintln().showPrintln()
/****************************************/
val infoValue:String? = null //infoValue It's a nullable type String String?== Null type
infoValue.outputStrValueF(" The default value is 1")
val name = "yuknight"
name.outputStrValueFget(" The default value is 2")
println(infoValue.outputStrValueFget(" The default value is 3"))
println(name.outputStrValueFget(" The default value is 4"))
}
边栏推荐
- 发挥创客教育空间的广泛实用性
- CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
- buureservewp(2)
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Network learning (III) -- highly concurrent socket programming (epoll)
- Leetcode simple question: find the K beauty value of a number
- Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
- 调用 pytorch API完成线性回归
- Pytoch (VI) -- model tuning tricks
猜你喜欢
Make LIVELINK's initial pose consistent with that of the mobile capture actor
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
利用 Helm 在各类 Kubernetes 中安装 Rainbond
运放电路的反馈电阻上并联一个电容是什么作用
【雅思口语】安娜口语学习记录 Part2
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Easy to understand SSO
Jmeter 的使用
Application of slip ring of shipborne radar antenna
Use of JMeter
随机推荐
快解析内网穿透为文档加密行业保驾护航
Application of slip ring of shipborne radar antenna
[step on the pit series] H5 cross domain problem of uniapp
Uniapp mobile terminal forced update function
Introduction à l'objet blob
Complex network modeling (III)
JS copy picture to clipboard read clipboard
Network learning (I) -- basic model learning
CDC (change data capture technology), a powerful tool for real-time database synchronization
eBPF Cilium实战(2) - 底层网络可观测性
DNS server configuration
buureservewp(2)
Myabtis_Plus
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Blob 對象介紹
【雅思口语】安娜口语学习记录 Part3
通俗易懂单点登录SSO
Zcmu--1396: queue problem (2)
运放电路的反馈电阻上并联一个电容是什么作用
Interview questions (CAS)