分类 安卓 下的文章

    fun t1(name:String) = flow{
        println("flow start : $name")
        for(i in 0..3){
            kotlinx.coroutines.delay(100)
            emit("$name $i")// 向外面发一个数据
        }
    }

    runBlocking{// 所有线程一起运行(顺序的话就删除launch就好)执行线程(流)执行完
        launch {
            t1("1").collect {
                println("get1 $it")
            }
        }

        // 开启第二个流
        launch {
            t1("2").collect {
                println("get2 $it")
            }
        }

        // 同一个流监听两次
        launch {
            val t = t1("3")
            t.collect {
                println("get4 $it")
            }
            t.collect {
                println("get4 $it")
            }
        }
    }

快速创建:

(1..3).asFlow().collect { value -> println(value) }

但是你不能修改延迟,所以可以增加map处理,或者增加过虑 filter

    runBlocking {
        launch {
                (1..10).asFlow() // 一个请求流
                    .filter {
                        it % 2 == 0 // 只显示偶数
                    }
                    .map {
                        // 每个个过来的都延迟1000秒
                        delay(100)
                        "hi $it";
                    }
                    .collect { response -> // 指定变量名字,不再是it
                        println(response)
                        println(response)
                        // 这里延迟也有用的
                        delay(1000)
                    }
        }

增加数据:

(1..3).asFlow() // 一个请求流
.transform { request ->
    emit("Making request $request") 
    emit(performRequest(request)) 
}
.collect { response -> println(response) }

限制数据,例如只处理两次:

fun numbers(): Flow<Int> = flow {
    try {                          
        emit(1)
        emit(2) 
        println("This line will not execute")
        emit(3)    
    } finally {
        println("Finally in numbers")
    }
}

fun main() = runBlocking<Unit> {
    numbers() 
        .take(2) // 只获取前两个
        .collect { value -> println(value) }
} 

更多参考这个

// All examples create a function object that performs upper-casing.
// So it's a function from String to String

val upperCase1: (String) -> String = { str: String -> str.toUpperCase() } // 1

val upperCase2: (String) -> String = { str -> str.toUpperCase() }         // 2

val upperCase3 = { str: String -> str.toUpperCase() }                     // 3

// val upperCase4 = { str -> str.toUpperCase() }                          // 4

val upperCase5: (String) -> String = { it.toUpperCase() }                 // 5

val upperCase6: (String) -> String = String::toUpperCase                  // 6

println(upperCase1("hello"))
println(upperCase2("hello"))
println(upperCase3("hello"))
println(upperCase5("hello"))
println(upperCase6("hello"))

fun main() {

test("fd1",1,2)
test("fd2")

// 注意这里,要调用 toIntArray,如果直接传*arg是有类型错误
val arg = arrayOf(3,4)
test("fd3", *arg.toIntArray())

}

fun test(name:String,vararg args:Int){

println("$name size ${args.size}")
print(name)
for(it in args){
    print(" ")
    print(it)
}
print("\n")

}

这里Mammal 类外部就是不可见的

sealed class Mammal(val name: String)                                                   // 1

class Cat(val catName: String) : Mammal(catName)                                        // 2
class Human(val humanName: String, val job: String) : Mammal(humanName)

fun greetMammal(mammal: Mammal): String {
    when (mammal) {                                                                     // 3
        is Human -> return "Hello ${mammal.name}; You're working as a ${mammal.job}"    // 4
        is Cat -> return "Hello ${mammal.name}"                                         // 5     
    }                                                                                   // 6
}

fun main() {
    println(greetMammal(Human("fd", "IT")))
    println(greetMammal(Cat("Snowy")))
}

常用方法:

fun main() {
    for(i in 0..3) {             // 0123 从0到3
        print(i)
    }
    print(" ")

    for(i in 0 until 3) {        // 012 从0到2(不包3)
        print(i)
    }
    print(" ")

    for(i in 2..8 step 2) {      // 2468 步进是2 
        print(i)
    }
    print(" ")

    for (i in 3 downTo 0) {      // 3210 倒数
        print(i)
    }
    print(" ")

}

一些另类用法:

for (c in 'a'..'d') {        // 1
    print(c)
}
print(" ")

for (c in 'z' downTo 's' step 2) { // 2
    print(c)
}
print(" ")

还可以用在if 判断上

val x = 2
if (x in 1..5) {            // 1
    print("x is in range from 1 to 5")
}
println()

if (x !in 6..10) {          // 2
    print("x is not in range from 6 to 10")
}