我的kotlin学习之旅-day5-2020-12-24

学习内容

1.枚举
2.内部类
3.函数式表达式,lambda表达式
4.Kotlin中的特殊函数

package day1

fun main(args:Array<String>) {
    val north = DIRECTION.NORTH;
    println(north.name)
    println(north.ordinal)
    val c = Color.BLUE
    println(c)
    println(c.rgb)
    val a = NestedClassDemo.Outer.Nested().getTwo()
    println(a)
    val b = NestedClassDemo.Outer.Nested.Nested1().getThree()
    println(b)
    val iner = NestedClassDemo.Outer().UseOuter().accessOuter()
    println(iner)
    NestedClassDemo.Outer().doRun()
    doStop()
    doWait()
    doNotify()
    testFilter()
    testRun()
    testApply()
    testLetFun()
    testAlsoFun()
    testWithFun()
}
/**
 * with方法
 */

fun testWithFun(){
    val list = mutableListOf<String>()
    list.add("A")
    list.add("B")
    list.add("C")
    println(list)
    with(ArrayList<String>()){
        add("A")
        add("B")
        add("C")
    }.let {
        println(it)
    }
}
/**
 * also方法
 */
fun testAlsoFun(){
    val a = "ABC".also { println(it) }
    println(a)
    a.let { println(it) }
}
/**
 * let方法
 */
fun testLetFun(){
    1.let { println(it) }
    "AbC".let { println(it) }
    testFun().let {
        println(it)
    }
}

/**
 * apply 方法
 *
 */

fun testApply(){
    val list = mutableListOf<String>()
    list.add("A")
    list.add("B")
    list.add("C")
    println("普通写法。。。")
    println(list)
    val a = ArrayList<String>().apply {
        add("A")
        add("B")
        add("C")
    }
    a.let { println(it) }
}
/**
 * 特殊函数
 */
fun testFun():String{
    println("Test fun 函数执行")
    return "这是一个函数的返回值"
}

/**
 * run方法
 */
fun testRun(){
    testFun()
    run { testFun() }
    run({ testFun()})
    run { println("AAAAA") }
}

/**
 * 函数式编程
 */

fun testFilter(){
    val arr = arrayOf(1,2,3,4,5,6,7)
    println(arr.filter { it%2==1 })
}

/**
 * Lambda表达式声明线程
 */
fun doStop(){
    var isRunning = true
    Thread({
        isRunning = false
        println("Do stop,not run")
    }).start()
}

fun doWait(){
    var isRunning = true
    val wait = Runnable {
        isRunning = false
        println("Do wait...")
    }
    Thread(wait).start()
}

fun doNotify(){
    var isRunning = true
    val wait = {
        isRunning = false
        println("Do notify...")
    }
    Thread(wait).start()
}

/**
 * 内部类
 */

class NestedClassDemo{
    class Outer{
        private val zero:Int = 0
        val one:Int = 1
        //嵌套内部类可以访问外部成员
        inner class UseOuter{
            fun accessOuter(){
                println(zero)
                println(one)
            }
        }
       fun doRun(){
           //匿名内部类
            Thread(Runnable { println("inner class run") }).start()
        }
        class Nested{
            //没有持有外部类的引用,无法访问zero,one
            fun getTwo() = 2
            class Nested1 {
                fun getThree() = 3
            }
        }
    }
}


/**
 * 枚举类
 */
enum class DIRECTION{
    NORTH,WEST,EAST,SOUTH
}

enum class Color(val rgb:Int) {
    RED(0xFF0000),
    GREEN(0x00FF00),
    BLUE(0x0000FF)
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容