当前位置:网站首页>Use of any superclass and generic extension function in kotlin
Use of any superclass and generic extension function in kotlin
2022-07-07 08:14:00 【yu-Knight】
//Kotlin in Any Use of superclasses and generic extension functions
data class ResponseResult1(val msg: String, val code: Int)
data class ResponseResult2(val msg: String, val code: Int)
// Pair superclass Any Extend a function
fun Any.showPrintlnContent() = println(" The current content is : $this")
// Pair superclass Any Extend a function , When calling, it can be called in a chain
fun Any.showPrintln(): Any {
println(" The current content is :$this")
return this
}
/********************************************************/
// For generic extensions
fun <T> T.showContentInfo() {
println("${if (this is String) " The string length is :$length" else " It's not a string , The content is :$this"}")
}
fun <T> T.showTime() = println(" The current call time is :${System.currentTimeMillis()}, The content is :$this")
// function
fun commonFun() {}
fun main() {
ResponseResult1("login success", 200).showPrintlnContent()
ResponseResult2("login success", 200).showPrintlnContent()
"yuknight".showPrintlnContent()
val number = 14555
number.showPrintlnContent()
val number1 = 33.44
number1.showPrintlnContent()
val number2 = 44.2f
number2.showPrintlnContent()
// Extension function return Any, Chain transfer can be carried out
"yuknight".showPrintln().showPrintln().showPrintln()
/************************************************/
"yuknight".showContentInfo()
val num = 14555
num.showContentInfo()
val num1 = 33.44
num1.showContentInfo()
val num2 = 44.2f
num2.showContentInfo()
commonFun().showContentInfo()
println()
"yuknight".showTime()
val nu = 14555
nu.showTime()
val nu1 = 33.44
nu1.showTime()
val nu2 = 44.2f
nu2.showTime()
commonFun().showTime()
}
边栏推荐
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
- Vulnerability recurrence easy_ tornado
- Jmeter 的使用
- JS复制图片到剪切板 读取剪切板
- [quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
- B. Value sequence thinking
- Excel import function of jeesite form page
- Myabtis_Plus
猜你喜欢
【雅思口语】安娜口语学习记录 Part2
Make LIVELINK's initial pose consistent with that of the mobile capture actor
uniapp 移动端强制更新功能
藏书馆App基于Rainbond实现云原生DevOps的实践
Niu Mei's mathematical problem --- combinatorial number
王爽 《汇编语言》之寄存器
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
海信电视开启开发者模式
在 Rainbond 中一键安装高可用 Nacos 集群
Network learning (II) -- Introduction to socket
随机推荐
Excel import function of jeesite form page
Record a stroke skin bone error of the skirt
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
Lattice coloring - matrix fast power optimized shape pressure DP
Basic use of CTF web shrink template injection nmap
jeeSite 表单页面的Excel 导入功能
利用 Helm 在各类 Kubernetes 中安装 Rainbond
机器人教育在动手实践中的真理
Hisense TV starts the developer mode
Zcmu--1492: problem d (C language)
Application of slip ring of shipborne radar antenna
积分商城管理系统中应包含的四大项
解析机器人科技发展观对社会研究论
It took "7" years to build the robot framework into a micro service
在Rainbond中一键部署高可用 EMQX 集群
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Network learning (III) -- highly concurrent socket programming (epoll)
Blob 对象介绍