当前位置:网站首页>Kotlin基础学习 15
Kotlin基础学习 15
2022-07-02 03:20:00 【学习不停息】
目录
1.Kotlin语言定义扩展函数学习
class KtBase113(val name : String,val age : Int,val sex : Char)
//增加扩展函数
fun KtBase113.show(){
println("我是show函数,name:$name,age:$age,sex:$sex")
}
fun KtBase113.getInfo() = "我是getInfo函数,name:$name,age:$age,sex:$sex"
fun String.addExtAction(number:Int) = this + "@".repeat(number)
fun String.showStr() = println(this)
// TODO 113.Kotlin语言定义扩展函数学习
fun main(){
val p = KtBase113("张三",22,'男')
p.show()
println(p.getInfo())
println("Bxb".addExtAction(6))
println("Bxb".addExtAction(2))
//这个是我的日志信息
"Bxb".showStr()
}2.Kotlin语言的超类上定义扩展函数学习
data class ResponseResult1(val msg:String,val code : Int)
data class ResponseResult2(val msg:String,val code : Int)
data class ResponseResult3(val msg:String,val code : Int)
data class ResponseResult4(val msg:String,val code : Int)
//对超类进行一个函数扩展
fun Any.showPrintlnContent() = println("当前内容是:$this")
fun Any.showPrintlnContent2() : Any{
println("当前内容是:$this")
return this
}
// TODO 114.Kotlin语言的超类上定义扩展函数学习
fun main(){
ResponseResult1("login success",200).showPrintlnContent()
ResponseResult2("login success",200).showPrintlnContent()
ResponseResult3("login success",200).showPrintlnContent()
ResponseResult4("login success",200).showPrintlnContent()
"Bxb".showPrintlnContent()
val number1 = 666666
number1.showPrintlnContent()
val number2 = 456.32f
number2.showPrintlnContent()
val sex = '男'
sex.showPrintlnContent()
println()
'女'.showPrintlnContent2().showPrintlnContent2().showPrintlnContent2()
"Bxb is OK".showPrintlnContent2().showPrintlnContent2()
//KT内置的扩展函数,被我们重新定义,属于覆盖,会优先调用我们自己定义的扩展函数
}
3.Kotlin语言的泛型扩展函数学习
//1.String类型输出长度
fun <T> T.showContentInfo() = println("${if (this is String) "你的字符串长度是:$length" else "你不是字符串 你的内容是:$this"}")
//2.显示调用时间
fun <I> I.showTime() = println("你当前调用的时间是:${System.currentTimeMillis()},内容是:$this")
//3.显示调用者的内容
fun <INPUTTYPE> INPUTTYPE.showTypesAction() =
when(this) {
is String -> "原来你是String类型"
is Int -> "原来你是Int类型"
is Char -> "原来你是Char类型"
is Float -> "原来你是Float类型"
is Double -> "原来你是Double类型"
is Boolean -> "原来你是Boolean类型"
is Unit -> "原来你是Unit类型"
else -> "未知类型"
}
fun commenFun(){}
// TODO 115.Kotlin语言的泛型扩展函数学习
fun main(){
145.showContentInfo()
'C'.showContentInfo()
false.showContentInfo()
145.3f.showContentInfo()
145.56.showContentInfo()
"Bxb".showContentInfo()
commenFun().showContentInfo()
println()
145.showTime()
'C'.showTime()
false.showTime()
145.3f.showTime()
145.56.showTime()
"Bxb".showTime()
commenFun().showTime()
println()
println(145.showTypesAction())
println('C'.showTypesAction())
println(false.showTypesAction())
println(145.3f.showTypesAction())
println(145.56.showTypesAction())
println("Bxb".showTypesAction())
println(commenFun().showTypesAction())
}4.Kotlin语言的标准函数与泛型扩展函数
// TODO 116.Kotlin语言的标准函数与泛型扩展函数
fun main(){
val r : String = "Bxb".mLet {
it
true
"OK"
}
123.mLet{
it
}
'C'.mLet {
it
}
//万能类型,任何类型,都可以使用mLet
val r2 : String = "Bxb2".let {
it
1542.6f
"Bxb"
}
}
// private 私有化
// inline 我们的函数是高阶函数,所以用到内联,做lambda的优化,性能提高
// fun<I,O> 在函数中,申明两个泛型,函数泛型 I输入 Input,O输出Output
// I.mLet 对I输入Input进行函数扩展,扩展函数的名称是mLet,意味着,所有的类型,万能类型,都可以用 xxx.mLet
// lambda: (I) -> O I输入,O输出
// :O 会根据用户的返回类型,变化而变化
// lambda(this) I 进行函数扩展,在整个扩展函数里,this==I本身
private inline fun<I,O> I.mLet(lambda:(I) -> O) : O = lambda(this)5.Kotlin语言的扩展属性
val myStr : String = "AAA"
val String.myInfo:String
get() = "Bxb"
//打印输出 并且 链式调用(只有String有这样的资格)
fun String.showPrintlin() : String{
println("打印输出 并且 链式调用(只有String有这样的资格):内容$this")
return this
}
val String.StringAllInfoValueVal
get() = "当前${System.currentTimeMillis()}这个时间点被调用了一次,当前的值是:$this,当前字符串长度是:$length"
// TODO 117.Kotlin语言的扩展属性
fun main(){
val str : String = "ABC"
println(str.myInfo)
str.showPrintlin().showPrintlin().showPrintlin()
str.myInfo.showPrintlin().showPrintlin()
"Bxb帅哥".StringAllInfoValueVal//扩展属性
.showPrintlin().showPrintlin() //扩展函数
}
边栏推荐
- MySQL advanced (Advanced) SQL statement (II)
- Just a few simple steps - start playing wechat applet
- el-table的render-header用法
- 2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
- 2022-2028 global manual dental cleaning equipment industry research and trend analysis report
- 2022-2028 global wood vacuum coating machine industry research and trend analysis report
- Render header usage of El table
- Apple added the first iPad with lightning interface to the list of retro products
- Start a business
- One of the future trends of SAP ui5: embrace typescript
猜你喜欢
![寻找重复数[抽象二分/快慢指针/二进制枚举]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
寻找重复数[抽象二分/快慢指针/二进制枚举]

How to establish its own NFT market platform in 2022

MSI announced that its motherboard products will cancel all paper accessories

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

C reflection practice

2022-2028 global wood vacuum coating machine industry research and trend analysis report

2022-2028 global human internal visualization system industry research and trend analysis report

Design details of SAP e-commerce cloud footernavigationcomponent

2022-2028 global deep sea generator controller industry research and trend analysis report

Verilog 过程赋值 区别 详解
随机推荐
Qualcomm platform WiFi -- P2P issue (2)
OSPF LSA message parsing (under update)
Redis cluster
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Competition and adventure burr
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
How to establish its own NFT market platform in 2022
Spark Tuning
Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
verilog REG 寄存器、向量、整数、实数、时间寄存器
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
焱融看 | 混合云时代下,如何制定多云策略
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Discussion on related configuration of thread pool
2022-2028 global deep sea generator controller industry research and trend analysis report
Uniapp uses canvas to generate posters and save them locally
高并发场景下缓存处理方案
JS introduction < 1 >
跟着CTF-wiki学pwn——ret2shellcode