当前位置:网站首页>Kotlin basic learning 16
Kotlin basic learning 16
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin Nullable type extension function of language
2.Kotlin Linguistic infix keyword
3.Kotlin Definition extension file of language
4.Kotlin Language rename extended learning
6.Kotlin The transformation function of language map
7.Kotlin The transformation function of language -flatmap
1.Kotlin Nullable type extension function of language
// Yes String?== Nullable type Function extension , And there are backup values
fun String?.outputStringValueFun(defaultValue:String) = println(this ?: defaultValue)
fun String?.outputStringValueFunGet(defaultValue: String) = if(this == null) defaultValue else this
// TODO 118.Kotlin Nullable type extension function of language
fun main(){
val infoValue : String ?= null
infoValue.outputStringValueFun(" I'm the default 1")
val name = "Derry"
name.outputStringValueFun(" I'm the default 2")
println(infoValue.outputStringValueFunGet(" I'm the default 3"))
println(name.outputStringValueFunGet(" I'm the default 4"))
}
2.Kotlin Linguistic infix keyword
private infix fun<C1,C2>C1.gogogo(c2:C2){
println(" Our infix expression , The content of a parameter is :$this")
println(" Our infix expression , The content of the second parameter is :$c2")
}
// TODO 118.Kotlin Linguistic infix keyword
//infix == Infix expression It simplifies the code
fun main(){
// Here we are map Built in infix expression
mapOf(" zero ".to(0))
mapOf(" One " to 1)
mapOf(" Two " to 2)
mapOf(" 3、 ... and " to 3)
// We wrote our own infix expression
123 gogogo ' male '
"Bxb".gogogo('M')
"Bxb2" gogogo 'M'
}
3.Kotlin Definition extension file of language
// Guide pack
import com.bxb.s6.bxb.randomItemValue
import com.bxb.s6.bxb.randomItemValuePrintln
// TODO 120.Kotlin Definition extension file of language
fun main(){
val list : List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val set:Set<Double> = setOf(15.6,456.5,784.6)
// If you don't need to extend the file
println(list.shuffled().first())
println(set.shuffled().first())
// Use extension files
println(list.randomItemValue())
println(set.randomItemValue())
list.randomItemValuePrintln()
set.randomItemValuePrintln()
}
4.Kotlin Language rename extended learning
// Guide pack
import com.bxb.s6.bxb.randomItemValue as g
import com.bxb.s6.bxb.randomItemValuePrintln as f
// TODO 120.Kotlin Language rename extended learning
fun main(){
val list : List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val set:Set<Double> = setOf(15.6,456.5,784.6)
// Use extension files
println(list.g())
println(set.g())
list.f()
set.f()
}
5.Kotlin Linguistic DSL Study
import java.io.File
class Context{
val info = " I am Bxb"
val name = "DDD"
fun toast(str:String) = println("toast:$str")
}
inline fun Context.apply5(lambda:Context.(String) -> Unit):Context{
lambda(info)
return this
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
inline fun File.applyFile(action:(String,String?) -> Unit) : File{
setWritable(true)
setReadable(true)
action(name,readLines()[0])
return this
}
// TODO 123.Kotlin Linguistic DSL Study
fun main(){
val context:Context = Context().apply5 {
//it == String == " I am Bxb"
println(" my it yes $it, my this yes :$this")
toast("success")
toast(it)
toast(name)
true
}.apply5 { }.apply5 { }.apply5 { }
println()
println(" I always output Context In itself :"+context.info)
println()
val file:File = File("D:\\a.txt")
.applyFile { fileName, data ->
println(" Your file name is :$fileName, The data in your file is :$data")
println(" Your file name is :$fileName, The data in your file is :$data")
println(" Your file name is :$fileName, The data in your file is :$data")
}.applyFile { a, b -> }.applyFile { a, b -> }
println(" I always output File In itself :${file.name}")
}
6.Kotlin The transformation function of language map
// TODO 124.Kotlin The transformation function of language map
fun main(){
val list = listOf(" Zhang San "," Li Si "," Wang Wu ")
// principle : Is to take you Anonymous functions The return value of the last line Add a new collection , The generic type of scheming and is R, And return a new set
val list2 : List<Int> = list.map {
"[$it]"
88
}
println(list2)
// purpose : and RxJava The idea of , As like as two peas
val list3 : List<String> = list.map {
" Name is :$it"
}.map {
"$it, The length of the text is :${it.length}"
}.map {
"[$it]"
}
for (s in list3){
print("$s ")
}
println()
list.map {
" Name is :$it"
}.map {
"$it, The length of the text is :${it.length}"
}.map {
"[$it]"
}.map {
print("$it ")
}
}
7.Kotlin The transformation function of language -flatmap
// TODO 125.Kotlin The transformation function of language -flatmap
// TODO flatmap amount to List<List<String>> A collection of collections , There are nested relationships
fun main(){
val list:List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val newList :List<String> = list.map {
" Your name is :$it"
}.map {
"$it, The length of your text is :${it.length}"
}.flatMap {
listOf("$it I'm learning C++","$it I'm learning Java","$it I'm learning Kotlin")
}
println(newList)
println()
val newList2:List<String> = list.flatMap {
listOf("$it I'm learning C++","$it I'm learning Java","$it I'm learning Kotlin")
}
println(newList2)
}
边栏推荐
- Framing in data transmission
- Pointer array & array pointer
- JS introduction < 1 >
- C reflection practice
- QT environment generates dump to solve abnormal crash
- PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
- Verilog 过程赋值 区别 详解
- OSPF LSA message parsing (under update)
- Go execute shell command
- Uniapp uses canvas to generate posters and save them locally
猜你喜欢
Discussion on related configuration of thread pool
C reflection practice
[HCIA continuous update] working principle of OSPF Protocol
This article describes the step-by-step process of starting the NFT platform project
verilog 并行块实现
高并发场景下缓存处理方案
C shallow copy and deep copy
SAML2.0 notes (I)
表单自定义校验规则
How to establish its own NFT market platform in 2022
随机推荐
MySQL advanced (Advanced) SQL statement (II)
QT environment generates dump to solve abnormal crash
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Verilog reg register, vector, integer, real, time register
C # joint halcon out of halcon Environment and various Error Reporting and Resolution Experiences
Failed to upgrade schema, error: “file does not exist
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
Framing in data transmission
焱融看 | 混合雲時代下,如何制定多雲策略
C shallow copy and deep copy
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
tarjan2
Verilog avoid latch
ThreadLocal详解
aaaaaaaaaaaaa
寻找重复数[抽象二分/快慢指针/二进制枚举]
JIT deep analysis
JS introduction < 1 >
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
Qualcomm platform wifi-- WPA_ supplicant issue