当前位置:网站首页>Kotlin basic learning 15
Kotlin basic learning 15
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin Language definition extension function learning
2.Kotlin Define extended function learning on the super class of language
3.Kotlin Language generic extension function learning
4.Kotlin Standard functions and generic extension functions of language
5.Kotlin Extended properties of language
1.Kotlin Language definition extension function learning
class KtBase113(val name : String,val age : Int,val sex : Char)
// Add extension function
fun KtBase113.show(){
println(" I am a show function ,name:$name,age:$age,sex:$sex")
}
fun KtBase113.getInfo() = " I am a getInfo function ,name:$name,age:$age,sex:$sex"
fun String.addExtAction(number:Int) = this + "@".repeat(number)
fun String.showStr() = println(this)
// TODO 113.Kotlin Language definition extension function learning
fun main(){
val p = KtBase113(" Zhang San ",22,' male ')
p.show()
println(p.getInfo())
println("Bxb".addExtAction(6))
println("Bxb".addExtAction(2))
// This is my log information
"Bxb".showStr()
}
2.Kotlin Define extended function learning on the super class of language
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)
// Extend the superclass with a function
fun Any.showPrintlnContent() = println(" The current content is :$this")
fun Any.showPrintlnContent2() : Any{
println(" The current content is :$this")
return this
}
// TODO 114.Kotlin Define extended function learning on the super class of language
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 = ' male '
sex.showPrintlnContent()
println()
' Woman '.showPrintlnContent2().showPrintlnContent2().showPrintlnContent2()
"Bxb is OK".showPrintlnContent2().showPrintlnContent2()
//KT Built in extension function , Redefined by us , It belongs to covering , The extension function defined by ourselves will be called first
}
3.Kotlin Language generic extension function learning
//1.String Type output length
fun <T> T.showContentInfo() = println("${if (this is String) " Your string length is :$length" else " You are not a string Your content is :$this"}")
//2. Display the call time
fun <I> I.showTime() = println(" Your current call time is :${System.currentTimeMillis()}, The content is :$this")
//3. Display the contents of the caller
fun <INPUTTYPE> INPUTTYPE.showTypesAction() =
when(this) {
is String -> " So you are String type "
is Int -> " So you are Int type "
is Char -> " So you are Char type "
is Float -> " So you are Float type "
is Double -> " So you are Double type "
is Boolean -> " So you are Boolean type "
is Unit -> " So you are Unit type "
else -> " Unknown type "
}
fun commenFun(){}
// TODO 115.Kotlin Language generic extension function learning
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 Standard functions and generic extension functions of language
// TODO 116.Kotlin Standard functions and generic extension functions of language
fun main(){
val r : String = "Bxb".mLet {
it
true
"OK"
}
123.mLet{
it
}
'C'.mLet {
it
}
// Universal type , Any kind of , You can use mLet
val r2 : String = "Bxb2".let {
it
1542.6f
"Bxb"
}
}
// private Privatization
// inline Our function is a higher-order function , So we use inline , do lambda The optimization of the , Performance improvement
// fun<I,O> In the function , Declare two generics , The function of generic I Input Input,O Output Output
// I.mLet Yes I Input Input Function extension , The name of the extension function is mLet, signify , All types , Universal type , Both can be used. xxx.mLet
// lambda: (I) -> O I Input ,O Output
// :O According to the user's return type , Change and change
// lambda(this) I Function extension , In the whole extension function ,this==I In itself
private inline fun<I,O> I.mLet(lambda:(I) -> O) : O = lambda(this)
5.Kotlin Extended properties of language
val myStr : String = "AAA"
val String.myInfo:String
get() = "Bxb"
// Printout also call chaining ( Only String Have such qualifications )
fun String.showPrintlin() : String{
println(" Printout also call chaining ( Only String Have such qualifications ): Content $this")
return this
}
val String.StringAllInfoValueVal
get() = " At present ${System.currentTimeMillis()} This point in time is called once , The current value is :$this, The current string length is :$length"
// TODO 117.Kotlin Extended properties of language
fun main(){
val str : String = "ABC"
println(str.myInfo)
str.showPrintlin().showPrintlin().showPrintlin()
str.myInfo.showPrintlin().showPrintlin()
"Bxb handsome guy ".StringAllInfoValueVal// Extended attributes
.showPrintlin().showPrintlin() // spread function
}
边栏推荐
- QT environment generates dump to solve abnormal crash
- PMP personal sprint preparation experience
- Mmsegmentation series training and reasoning their own data set (3)
- Framing in data transmission
- Discrimination between sap Hana, s/4hana and SAP BTP
- Go execute shell command
- Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
- Halcon image rectification
- 《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
- Aaaaaaaaaaaa
猜你喜欢
SAML2.0 笔记(一)
Cache processing scheme in high concurrency scenario
This article describes the step-by-step process of starting the NFT platform project
高并发场景下缓存处理方案
Eight steps of agile development process
Continuous assignment of Verilog procedure
OSPF LSA message parsing (under update)
初出茅庐市值1亿美金的监控产品Sentry体验与架构
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
随机推荐
Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
Spark Tuning
数据传输中的成帧
Cache processing scheme in high concurrency scenario
This article describes the step-by-step process of starting the NFT platform project
Competition and adventure burr
表单自定义校验规则
Apple added the first iPad with lightning interface to the list of retro products
verilog REG 寄存器、向量、整数、实数、时间寄存器
知物由学 | 自监督学习助力内容风控效果提升
js生成随机数
How to establish its own NFT market platform in 2022
Possible causes of runtime error
Verilog parallel block implementation
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
Named block Verilog
Verilog 过程赋值 区别 详解
命名块 verilog
uniapp 使用canvas 生成海报并保存到本地