当前位置:网站首页>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()
}
边栏推荐
- Network learning (I) -- basic model learning
- Complete linear regression manually based on pytoch framework
- Complex network modeling (II)
- Uniapp mobile terminal forced update function
- JS复制图片到剪切板 读取剪切板
- game攻防世界逆向
- 电池、电机技术受到很大关注,反而电控技术却很少被提及?
- 积分商城管理系统中应包含的四大项
- buureservewp(2)
- [quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
猜你喜欢
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
通俗易懂单点登录SSO
使用 Nocalhost 开发 Rainbond 上的微服务应用
在Rainbond中一键部署高可用 EMQX 集群
快解析内网穿透助力外贸管理行业应对多种挑战
CDC (change data capture technology), a powerful tool for real-time database synchronization
复杂网络建模(一)
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
OpenVSCode云端IDE加入Rainbond一体化开发体系
王爽 《汇编语言》之寄存器
随机推荐
Leetcode 187 Repeated DNA sequence (2022.07.06)
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
饥荒云服管理脚本
Fast parsing intranet penetration escorts the document encryption industry
Roulette chart 2 - writing of roulette chart code
The largest 3 same digits in the string of leetcode simple question
OpenJudge NOI 2.1 1752:鸡兔同笼
Recursive method to verify whether a tree is a binary search tree (BST)
Recursive method constructs binary tree from middle order and post order traversal sequence
调用 pytorch API完成线性回归
基于Pytorch 框架手动完成线性回归
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Empire CMS collection Empire template program general
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Complete linear regression manually based on pytoch framework
在 Rainbond 中一键安装高可用 Nacos 集群
Rainbond结合NeuVector实践容器安全管理
解读创客思维与数学课程的实际运用
ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)