当前位置:网站首页>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()
}边栏推荐
- 发挥创客教育空间的广泛实用性
- 追风赶月莫停留,平芜尽处是春山
- buureservewp(2)
- 机器人教育在动手实践中的真理
- 调用 pytorch API完成线性回归
- Leetcode simple question: find the K beauty value of a number
- Bayes' law
- Complex network modeling (III)
- 雅思考试自己的复习进度以及方法使用【日更版】
- The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
猜你喜欢

Qinglong panel - today's headlines

Content of string

What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
![[step on the pit series] H5 cross domain problem of uniapp](/img/53/bd836a5c5545f51be929d8d123b961.png)
[step on the pit series] H5 cross domain problem of uniapp

JS copy picture to clipboard read clipboard

电池、电机技术受到很大关注,反而电控技术却很少被提及?

【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)

Myabtis_ Plus

Unityhub cracking & unity cracking

Lua 编程学习笔记
随机推荐
Recursive method to construct binary tree from preorder and inorder traversal sequence
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Uniapp mobile terminal forced update function
Codeforce c.strange test and acwing
OpenJudge NOI 2.1 1752:鸡兔同笼
Empire CMS collection Empire template program general
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Leetcode medium question my schedule I
JS quick start (I)
饥荒云服管理脚本
数据库实时同步利器——CDC(变化数据捕获技术)
Fast parsing intranet penetration escorts the document encryption industry
Linux Installation MySQL 8.0 configuration
Leetcode 187 Repeated DNA sequence (2022.07.06)
CDC (change data capture technology), a powerful tool for real-time database synchronization
buureservewp(2)
[untitled]
基于Pytorch 框架手动完成线性回归
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)