当前位置:网站首页>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)
}
边栏推荐
- QT environment generates dump to solve abnormal crash
- Custom classloader that breaks parental delegation
- [C Advanced] brother Peng takes you to play with strings and memory functions
- Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
- Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
- On redis (II) -- cluster version
- Form custom verification rules
- Force deduction daily question 540 A single element in an ordered array
- Verilog 过程连续赋值
- Kotlin基础学习 17
猜你喜欢
[C Advanced] brother Peng takes you to play with strings and memory functions
Start a business
halcon图像矫正
Design details of SAP e-commerce cloud footernavigationcomponent
C reflection practice
Verilog 时序控制
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
MMSegmentation系列之训练与推理自己的数据集(三)
Uniapp uses canvas to generate posters and save them locally
焱融看 | 混合雲時代下,如何制定多雲策略
随机推荐
V-model of custom component
Verilog 时序控制
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
QT environment generates dump to solve abnormal crash
Spark Tuning
Detailed explanation of ThreadLocal
汇率的查询接口
Cache processing scheme in high concurrency scenario
Intersection vengraph
verilog REG 寄存器、向量、整数、实数、时间寄存器
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
Go执行shell命令
Just a few simple steps - start playing wechat applet
js生成随机数
What is hybrid web containers for SAP ui5
OSPF LSA message parsing (under update)
PMP personal sprint preparation experience
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
Grpc quick practice
只需简单几步 - 开始玩耍微信小程序