当前位置:网站首页>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)
}边栏推荐
- JIT deep analysis
- Verilog 过程赋值 区别 详解
- 数据传输中的成帧
- Verilog timing control
- Work hard all day long and be alert at sunset
- [golang] leetcode intermediate bracket generation & Full Permutation
- Qualcomm platform wifi-- WPA_ supplicant issue
- Qualcomm platform WiFi -- P2P issue (2)
- Retrofit's callback hell is really vulnerable in kotlin synergy mode
- 【JVM】创建对象的流程详解
猜你喜欢

Verilog 时序控制

Verilog timing control

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

焱融看 | 混合云时代下,如何制定多云策略

Cache processing scheme in high concurrency scenario

Mmsegmentation series training and reasoning their own data set (3)

The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added

《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真

高并发场景下缓存处理方案

小米青年工程师,本来只是去打个酱油
随机推荐
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
Design details of SAP e-commerce cloud footernavigationcomponent
Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
Pointer array & array pointer
MySQL connection query and subquery
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
Kotlin基础学习 17
[数据库]JDBC
What is the binding path of SAP ui5
Merge interval, linked list, array
Intersection of Venn graph
Download and use of the super perfect screenshot tool snipaste
SAML2.0 笔记(一)
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
Form custom verification rules
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
aaaaaaaaaaaaa
Possible causes of runtime error
Redis set command line operation (intersection, union and difference, random reading, etc.)