当前位置:网站首页>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
}
边栏推荐
猜你喜欢

Which of PMP and software has the highest gold content?

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

Pointer array & array pointer

Framing in data transmission

Knowing things by learning | self supervised learning helps improve the effect of content risk control

Uniapp uses canvas to generate posters and save them locally

C shallow copy and deep copy

In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews

Delphi xe10.4 installing alphacontrols15.12

小米青年工程师,本来只是去打个酱油
随机推荐
What is the binding path of SAP ui5
Verilog 避免 Latch
SAML2.0 笔记(一)
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Qualcomm platform WiFi -- Native crash caused by WiFi
PMP personal sprint preparation experience
[C Advanced] brother Peng takes you to play with strings and memory functions
汇率的查询接口
Verilog 过程连续赋值
4. Find the median of two positive arrays
MySQL connection query and subquery
In depth interpretation of pytest official documents (26) customized pytest assertion error information
QT environment generates dump to solve abnormal crash
IPhone 6 plus is listed in Apple's "retro products" list
Kotlin基础学习 15
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Mathematical calculation in real mode addressing