当前位置:网站首页>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
}
边栏推荐
- Common means of modeling: aggregation
- How to establish its own NFT market platform in 2022
- Failed to upgrade schema, error: “file does not exist
- Verilog 时序控制
- Load different fonts in QML
- uniapp 使用canvas 生成海报并保存到本地
- [数据库]JDBC
- MSI announced that its motherboard products will cancel all paper accessories
- 4. Find the median of two positive arrays
- 2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
猜你喜欢

Form custom verification rules

Halcon image rectification

Screenshot literacy tool download and use

OSPF LSA message parsing (under update)

Start a business

Generate random numbers that obey normal distribution

数据传输中的成帧

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

Gradle foundation | customize the plug-in and upload it to jitpack

知物由学 | 自监督学习助力内容风控效果提升
随机推荐
JDBC details
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
Custom classloader that breaks parental delegation
焱融看 | 混合雲時代下,如何制定多雲策略
V-model of custom component
Form custom verification rules
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
What is hybrid web containers for SAP ui5
Force deduction daily question 540 A single element in an ordered array
On redis (II) -- cluster version
MySQL connection query and subquery
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
表单自定义校验规则
Kotlin基础学习 17
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
verilog 并行块实现
Kotlin 基础学习13
< job search> process and signal