当前位置:网站首页>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)
}边栏推荐
- 3124. Word list
- Qualcomm platform WiFi -- P2P issue (2)
- Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
- Competition and adventure burr
- venn圖取交集
- C reflection practice
- Force deduction daily question 540 A single element in an ordered array
- 跟着CTF-wiki学pwn——ret2shellcode
- Mmsegmentation series training and reasoning their own data set (3)
- Gradle 笔记
猜你喜欢

One of the future trends of SAP ui5: embrace typescript

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Verilog 过程连续赋值

Pointer array & array pointer

【JVM】创建对象的流程详解
![Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
On redis (II) -- cluster version

ZABBIX API creates hosts in batches according to the host information in Excel files

Exchange rate query interface

Common means of modeling: aggregation
随机推荐
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
Verilog 时序控制
< job search> process and signal
只需简单几步 - 开始玩耍微信小程序
创业了...
PMP personal sprint preparation experience
寻找重复数[抽象二分/快慢指针/二进制枚举]
spark调优
Pointer array & array pointer
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Verilog state machine
aaaaaaaaaaaaa
PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
Kotlin基础学习 15
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
Grpc quick practice
[数据库]JDBC
venn图取交集
焱融看 | 混合雲時代下,如何制定多雲策略
[C Advanced] brother Peng takes you to play with strings and memory functions