当前位置:网站首页>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
}
边栏推荐
- Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
- PMP personal sprint preparation experience
- ThreadLocal详解
- PY3 link MySQL
- Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Download and use of the super perfect screenshot tool snipaste
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- 《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
- JDBC details
- Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Common means of modeling: aggregation
跟着CTF-wiki学pwn——ret2shellcode
Kubernetes cluster storageclass persistent storage resource core concept and use
Gradle foundation | customize the plug-in and upload it to jitpack
Verilog avoid latch
Generate random numbers that obey normal distribution
Exchange rate query interface
verilog 并行块实现
Detailed explanation of ThreadLocal
Discussion on related configuration of thread pool
随机推荐
Common means of modeling: aggregation
Competition and adventure burr
V-model of custom component
How to establish its own NFT market platform in 2022
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
Halcon image rectification
js生成随机数
Continuous assignment of Verilog procedure
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
命名块 verilog
Mathematical calculation in real mode addressing
Verilog state machine
Merge interval, linked list, array
Verilog 避免 Latch
spark调优
Aaaaaaaaaaaa
32, 64, 128 bit system
Gradle 笔记
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
< job search> process and signal