当前位置:网站首页>Kotlin基础学习 16
Kotlin基础学习 16
2022-07-02 03:20:00 【学习不停息】
目录
1.Kotlin语言的可空类型扩展函数
//对 String?== 可空类型的 进行函数扩展,并且有备用值
fun String?.outputStringValueFun(defaultValue:String) = println(this ?: defaultValue)
fun String?.outputStringValueFunGet(defaultValue: String) = if(this == null) defaultValue else this
// TODO 118.Kotlin语言的可空类型扩展函数
fun main(){
val infoValue : String ?= null
infoValue.outputStringValueFun("我是默认值1")
val name = "Derry"
name.outputStringValueFun("我是默认值2")
println(infoValue.outputStringValueFunGet("我是默认值3"))
println(name.outputStringValueFunGet("我是默认值4"))
}
2.Kotlin语言的infix关键字
private infix fun<C1,C2>C1.gogogo(c2:C2){
println("我们的中缀表达式,对一个参数的内容是:$this")
println("我们的中缀表达式,对二个参数的内容是:$c2")
}
// TODO 118.Kotlin语言的infix关键字
//infix == 中缀表达式 可以简化代码
fun main(){
//下面是我们map自带的中缀表达式
mapOf("零".to(0))
mapOf("一" to 1)
mapOf("二" to 2)
mapOf("三" to 3)
//我们自己写的中缀表达式
123 gogogo '男'
"Bxb".gogogo('M')
"Bxb2" gogogo 'M'
}
3.Kotlin语言的定义扩展文件
//导包
import com.bxb.s6.bxb.randomItemValue
import com.bxb.s6.bxb.randomItemValuePrintln
// TODO 120.Kotlin语言的定义扩展文件
fun main(){
val list : List<String> = listOf("张三","李四","王五")
val set:Set<Double> = setOf(15.6,456.5,784.6)
//如果不用扩展文件
println(list.shuffled().first())
println(set.shuffled().first())
//使用扩展文件
println(list.randomItemValue())
println(set.randomItemValue())
list.randomItemValuePrintln()
set.randomItemValuePrintln()
}
4.Kotlin语言的重命名扩展学习
//导包
import com.bxb.s6.bxb.randomItemValue as g
import com.bxb.s6.bxb.randomItemValuePrintln as f
// TODO 120.Kotlin语言的重命名扩展学习
fun main(){
val list : List<String> = listOf("张三","李四","王五")
val set:Set<Double> = setOf(15.6,456.5,784.6)
//使用扩展文件
println(list.g())
println(set.g())
list.f()
set.f()
}
5.Kotlin语言的DSL学习
import java.io.File
class Context{
val info = "我就是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语言的DSL学习
fun main(){
val context:Context = Context().apply5 {
//it == String == "我就是Bxb"
println("我的it是$it,我的this是:$this")
toast("success")
toast(it)
toast(name)
true
}.apply5 { }.apply5 { }.apply5 { }
println()
println("我始终是输出Context本身:"+context.info)
println()
val file:File = File("D:\\a.txt")
.applyFile { fileName, data ->
println("你的文件名是:$fileName,你的文件里面的数据是:$data")
println("你的文件名是:$fileName,你的文件里面的数据是:$data")
println("你的文件名是:$fileName,你的文件里面的数据是:$data")
}.applyFile { a, b -> }.applyFile { a, b -> }
println("我始终是输出File本身:${file.name}")
}
6.Kotlin语言的变换函数map
// TODO 124.Kotlin语言的变换函数map
fun main(){
val list = listOf("张三","李四","王五")
//原理:就是把你 匿名函数 最后一行的返回值 加入一个新的集合,心机和的泛型是R,并且返回新集合
val list2 : List<Int> = list.map {
"[$it]"
88
}
println(list2)
//用途:和RxJava的思路,一模一样
val list3 : List<String> = list.map {
"姓名是:$it"
}.map {
"$it,文字的长度是:${it.length}"
}.map {
"[$it]"
}
for (s in list3){
print("$s ")
}
println()
list.map {
"姓名是:$it"
}.map {
"$it,文字的长度是:${it.length}"
}.map {
"[$it]"
}.map {
print("$it ")
}
}
7.Kotlin语言的变换函数-flatmap
// TODO 125.Kotlin语言的变换函数-flatmap
// TODO flatmap 相当于 List<List<String>> 集合的集合,有嵌套关系
fun main(){
val list:List<String> = listOf("张三","李四","王五")
val newList :List<String> = list.map {
"你的姓名是:$it"
}.map {
"$it,你文字的长度是:${it.length}"
}.flatMap {
listOf("$it 在学习C++","$it 在学习Java","$it 在学习Kotlin")
}
println(newList)
println()
val newList2:List<String> = list.flatMap {
listOf("$it 在学习C++","$it 在学习Java","$it 在学习Kotlin")
}
println(newList2)
}
边栏推荐
- KL divergence is a valuable article
- 小米青年工程师,本来只是去打个酱油
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- C shallow copy and deep copy
- Grpc quick practice
- Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
- JIT deep analysis
- Merge interval, linked list, array
- Baohong industry | four basic knowledge necessary for personal finance
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
猜你喜欢
2022-2028 global deep sea generator controller industry research and trend analysis report
Use usedeferredvalue for asynchronous rendering
Baohong industry | 6 financial management models at different stages of life
MySQL connection query and subquery
只需简单几步 - 开始玩耍微信小程序
Verilog 过程赋值 区别 详解
Design details of SAP e-commerce cloud footernavigationcomponent
Verilog state machine
初出茅庐市值1亿美金的监控产品Sentry体验与架构
Delphi xe10.4 installing alphacontrols15.12
随机推荐
Design details of SAP e-commerce cloud footernavigationcomponent
Use usedeferredvalue for asynchronous rendering
[golang] leetcode intermediate bracket generation & Full Permutation
在QML中加载不同字体
Verilog reg register, vector, integer, real, time register
2022-2028 global human internal visualization system industry research and trend analysis report
Intersection of Venn graph
ZABBIX API creates hosts in batches according to the host information in Excel files
spark调优
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
3048. Number of words
C reflection practice
Grpc quick practice
Halcon image rectification
What is hybrid web containers for SAP ui5
Apple added the first iPad with lightning interface to the list of retro products
Verilog avoid latch
MSI announced that its motherboard products will cancel all paper accessories
Download and use of the super perfect screenshot tool snipaste