当前位置:网站首页>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
}
边栏推荐
- 知物由学 | 自监督学习助力内容风控效果提升
- Review materials of project management PMP high frequency examination sites (8-1)
- Framing in data transmission
- C#联合halcon脱离halcon环境以及各种报错解决经历
- What is the binding path of SAP ui5
- Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
- MMSegmentation系列之训练与推理自己的数据集(三)
- 跟着CTF-wiki学pwn——ret2shellcode
- Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
- What is hybrid web containers for SAP ui5
猜你喜欢

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

MySQL advanced (Advanced) SQL statement (II)

Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
![Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]

OSPF LSA message parsing (under update)

Sentry experience and architecture, a fledgling monitoring product with a market value of $100million

创业了...

Just a few simple steps - start playing wechat applet

Uniapp uses canvas to generate posters and save them locally

ZABBIX API creates hosts in batches according to the host information in Excel files
随机推荐
小米青年工程师,本来只是去打个酱油
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
MSI announced that its motherboard products will cancel all paper accessories
Grpc快速实践
Generate random numbers that obey normal distribution
焱融看 | 混合雲時代下,如何制定多雲策略
Spark Tuning
MySQL advanced (Advanced) SQL statement (II)
Force deduction daily question 540 A single element in an ordered array
Discussion on related configuration of thread pool
< job search> process and signal
Mmsegmentation series training and reasoning their own data set (3)
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
表单自定义校验规则
Verilog 状态机
Kotlin基础学习 16
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Start a business
[数据库]JDBC
流线线使用阻塞还是非阻塞