当前位置:网站首页>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() //扩展函数
}
边栏推荐
- Baohong industry | 6 financial management models at different stages of life
- Uniapp uses canvas to generate posters and save them locally
- 初出茅庐市值1亿美金的监控产品Sentry体验与架构
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Halcon image rectification
- Qualcomm platform wifi-- WPA_ supplicant issue
- 2022-2028 global manual dental cleaning equipment industry research and trend analysis report
- Baohong industry | four basic knowledge necessary for personal finance
- aaaaaaaaaaaaa
- MySQL advanced (Advanced) SQL statement (II)
猜你喜欢

Baohong industry | 6 financial management models at different stages of life
![[HCIA continuous update] overview of dynamic routing protocol](/img/03/83c883afb63b7c63f6879b5513bac3.jpg)
[HCIA continuous update] overview of dynamic routing protocol

Design details of SAP e-commerce cloud footernavigationcomponent

verilog 并行块实现

Force deduction daily question 540 A single element in an ordered array

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
![[JS reverse series] analysis of a customs publicity platform](/img/15/fdff7047e789d4e7c3c273a2f714f3.jpg)
[JS reverse series] analysis of a customs publicity platform
![[golang] leetcode intermediate bracket generation & Full Permutation](/img/93/ca38d97c721ccba2505052ef917788.jpg)
[golang] leetcode intermediate bracket generation & Full Permutation

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

Pointer array & array pointer
随机推荐
Delphi xe10.4 installing alphacontrols15.12
ORA-01547、ORA-01194、ORA-01110
[JS reverse series] analysis of a customs publicity platform
IPhone 6 plus is listed in Apple's "retro products" list
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Verilog 线型wire 种类
Screenshot literacy tool download and use
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
知物由学 | 自监督学习助力内容风控效果提升
Merge interval, linked list, array
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Verilog 状态机
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
MMSegmentation系列之训练与推理自己的数据集(三)
Qualcomm platform WiFi -- Native crash caused by WiFi
4. Find the median of two positive arrays
C#联合halcon脱离halcon环境以及各种报错解决经历
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions