当前位置:网站首页>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() //扩展函数
}
边栏推荐
- Form custom verification rules
- Spark Tuning
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- PMP personal sprint preparation experience
- Redis set command line operation (intersection, union and difference, random reading, etc.)
- [HCIA continuous update] working principle of OSPF Protocol
- Delphi xe10.4 installing alphacontrols15.12
- Work hard all day long and be alert at sunset
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- How to develop digital collections? How to develop your own digital collections
猜你喜欢
JS <2>
Verilog 过程连续赋值
MMSegmentation系列之训练与推理自己的数据集(三)
小米青年工程师,本来只是去打个酱油
汇率的查询接口
创业了...
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
Discrimination between sap Hana, s/4hana and SAP BTP
Just a few simple steps - start playing wechat applet
Baohong industry | 6 financial management models at different stages of life
随机推荐
QT environment generates dump to solve abnormal crash
[golang] leetcode intermediate bracket generation & Full Permutation
SAML2.0 notes (I)
Retrofit's callback hell is really vulnerable in kotlin synergy mode
el-table的render-header用法
Mathematical calculation in real mode addressing
ZABBIX API creates hosts in batches according to the host information in Excel files
JS <2>
2022-2028 global nano abrasive industry research and trend analysis report
PMP personal sprint preparation experience
Xiaomi, a young engineer, was just going to make soy sauce
This article describes the step-by-step process of starting the NFT platform project
[JVM] detailed description of the process of creating objects
2022-2028 global human internal visualization system industry research and trend analysis report
Download and use of the super perfect screenshot tool snipaste
Delphi xe10.4 installing alphacontrols15.12
[C Advanced] brother Peng takes you to play with strings and memory functions
[HCIA continuous update] working principle of OSPF Protocol
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
aaaaaaaaaaaaa