Extend.kt
inline fun <T, R> myWith1(receiver: T, block: T.(Any) -> R): R {
var result:R = block.invoke(receiver,101)
return result
}
inline fun <T, R> T.myWith2( block: T.(Any) -> R): R {
var result:R = block.invoke(this,"angleBody")
return result
}
inline fun <T, R> T.myWith3( block: T.(Any) -> R): R {
var result:R = block.invoke(this,12)
return result
}
inline fun <T, R> myWith4( block: (T,Any) -> R): R {
val person= Person(name_="NIUBI",age_=233)
var result:R = block.invoke( person as T ,"niubi")
return result
}
val TAG="HigherOrderFunctionSuspendActivity"
inline fun <T, R> T.myWith5( block: T.(T,Any) -> R): R {
Log.e(TAG, "T.myWith5 输出当前对象this=${this}") //T.myWith5 输出当前对象this=name:天使, age:12, sex:1
val person= Person(name_="newPer",age_=23) as T
var result:R = block.invoke(this,person,true)
return result
}
inline fun<T,R> T.myWith6(block:(T)->R):R{
// val result:R = block(this)
// return result
val result:R = block.invoke(this)//b把接受者对象this 作为参数进行传递 回调给lambda
return result
}
inline fun <T, R> T.myWith7( block: T.(T) -> R): R {
//var result:R = receiver.block()
//return result
var result:R = block.invoke(this,this)
return result
}
class HigherOrderFunctionSuspendActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?){
val myWith1Result :String = myWith1(receiver = person,block={
Log.e(TAG,"myWith1 输出当前对象this=${this}, it=$it") //myWith1 输出当前对象this=name:张三, age:20, sex:1, it=101
this.name="张四"
this.age=21
"Androider" //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith1Result=${myWith1Result}") //myWith1Result=Androider
val myWith2Result :Person = person.myWith2(block={
Log.e(TAG,"myWith2 输出当前对象this=${this}, it=$it") //myWith2 输出当前对象this=name:张四, age:21, sex:1, it=angleBody
this.name=it as String
this.age=22
this //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith2Result=${myWith2Result}") //myWith2Result=name:angleBody, age:22, sex:1
val myWith3Result: String= 100.myWith3(block = {
Log.e(TAG, "myWith3 输出当前对象this=${this}, it=$it") //myWith3 输出当前对象this=100, it=1.2
"FM.1080" //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith3Result=${myWith3Result}") //myWith3Result=FM.1080
val myWith32Result: Person = person.myWith3(block = {
Log.e(TAG, "myWith3 输出当前对象this=${this}, it=$it") //myWith3 输出当前对象this=name:angleBody, age:22, sex:1, it=12
this.name="天使"
this.age=it as Int
this //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith32Result=${myWith32Result}") //myWith32Result=name:天使, age:12, sex:1
val myWith4Result:Int = myWith4(block={pp:Person,str->
Log.e(TAG, "myWith4 输出当前对象pp=${pp}, str=$str") //myWith4 输出当前对象pp=name:NIUBI, age:233, sex:0, str=niubi
2024 //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith4Result=${myWith4Result}") //myWith4Result=2024
Log.e(TAG, " 输出当前对象person=${person.toString()}") // 输出当前对象person=name:天使, age:12, sex:1
val myWith5Result:Boolean = person.myWith5(block={newP:Person,bo:Any->
Log.e(TAG, "myWith5 this=${this},输出当前对象newP=${newP},bo=$bo") //myWith5 this=name:天使, age:12, sex:1,输出当前对象newP=name:newPer, age:23, sex:0,bo=true
var isChild:Boolean=false
if(bo as Boolean){
isChild=true
}
isChild //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith5Result=${myWith5Result}") //myWith5Result=true
val myWith6Result:Person = person.myWith6(block={
Log.e(TAG, "myWith6 输出lambda的参数it=${it}") //myWith6 输出lambda的参数it=name:天使, age:12, sex:1
it.name="llong"
it //把lambda最后一行作为返回值
})
Log.e(TAG, "myWith6Result=${myWith6Result}") // myWith6Result=name:llong, age:12, sex:1
val myWith7Result:String = person.myWith7(block={
Log.e(TAG, "myWith7 输出lambda的参数this=${this}, it=${it}") //myWith7 输出lambda的参数this=name:llong, age:12, sex:1, it=name:llong, age:12, sex:1
"kotlin"
})
Log.e(TAG, "myWith7Result=${myWith7Result}") //myWith7Result=kotlin
}
}