笔记:kotlin 学习第四天

  1. 中缀表达式

    /**
     * @ 文件名:   `01.中缀表达式`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:27
     * @ 描述:    infix
     * Tom sayHelloTo John
     */
    fun main(args: Array<String>) {
    
        val pair = "a" to 1
    
        val tom = Person()
        tom.sayHelloTo("John")
    
        tom sayHelloTo "John"//方法前面添加infix关键字
    }
    
    class Person {
        infix fun sayHelloTo(name: String) {
            "你好$name"
        }
    }
    
  1. 类委托

    /**
     * @ 文件名:   `02.类委托`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:27
     * @ 描述:    by
     * 小头爸爸委托大头儿子洗碗
     */
    fun main(args: Array<String>) {
        val smallHeadFather = SmallHeadFather()
        smallHeadFather.wash()
    }
    
    interface WashPower {
        fun wash()
    }
    
    class SmallHeadFather : WashPower by BigHeadSon()
    
    class BigHeadSon : WashPower {
        override fun wash() {
            println("大头儿子洗碗")
        }
    }
    
  2. 类委托实现二

    /**
     * @ 文件名:   `03.类委托实现二`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:27
     * @ 描述:    by
     */
    fun main(args: Array<String>) {
    
        val bigHeadSon = BigHeadSon()
        val smallHeadSon = SmallHeadSon()
    
        //val bigHeadFather = BigHeadFather(bigHeadSon)
        val bigHeadFather = BigHeadFather(smallHeadSon)
        bigHeadFather.wash()
    
    }
    
    interface WashPower {
        fun wash()
    }
    
    class SmallHeadSon : WashPower {
        override fun wash() {
            println("小头儿子开始洗碗")
        }
    }
    
    class BigHeadSon : WashPower {
        override fun wash() {
            println("大头儿子开始洗碗")
        }
    }
    
    class BigHeadFather(var washPower: WashPower) : WashPower by washPower
    
  1. 类委托功能加强

    /**
     * @ 文件名:   `04.类委托功能加强`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:31
     * @ 描述:
     */
    fun main(args: Array<String>) {
        val smallHeadSon = SmallHeadSon()
        val bigHeadSon = BigHeadSon()
    
        val bigHeadFather = BigHeadFather(smallHeadSon)
        //val bigHeadFather = BigHeadFather(bigHeadSon)
    
        bigHeadFather.wash()
    }
    
    interface WashPower {
        fun wash()
    }
    
    class SmallHeadSon : WashPower {
        override fun wash() {
            println("小头儿子开始洗碗")
        }
    }
    
    class BigHeadSon : WashPower {
        override fun wash() {
            println("大头儿子开始洗碗")
        }
    }
    
    class BigHeadFather(var washPower: WashPower) : WashPower by washPower {
        override fun wash() {
            println("付给洗碗的儿子一块钱")
            washPower.wash()
            println("碗洗的不错,下次还找你洗")
        }
    }
    
  1. 属性委托

    /**
     * @ 文件名:   `05.属性委托`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:41
     * @ 描述:    by
     * 妈妈帮忙保管压岁钱
     */
    
    fun main(args: Array<String>) {
        val bigHeadSon = BigHeadSon()
    
        bigHeadSon.money = 100//收到100块压岁钱
    
        println(bigHeadSon.money)//50 被代保管了一半
    
    }
    
    class BigHeadSon {
        var money: Int by Mother()
    }
    
    class Mother {
    
        var moneySon = 0
        var moneyMother = 0
    
        operator fun getValue(bigHeadSon: BigHeadSon, property: KProperty<*>): Int {
            return moneySon
        }
    
        operator fun setValue(bigHeadSon: BigHeadSon, property: KProperty<*>, i: Int) {
            moneySon = i / 2
            moneyMother = i / 2
        }
    }
    
  1. 惰性加载

    /**
     * @ 文件名:   `06.惰性加载`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 14:41
     * @ 描述:    by lazy {  }
     * 字段必须 val 修饰
     * 1,by lazy 放到成员变量中,可以单独存在
     * 2,by lazy 返回值就是最后一行
     * 3, by lazy 是线程安全的
     */
    
    fun main(args: Array<String>) {
        val lazy = Lazy()
        println(lazy.name)
        println(lazy.name)
        println(lazy.name)
    }
    
    class Lazy {
        val name: String by lazy {
            println("只初始化一次")
            "懒加载"
        }
    }
    
  1. 延迟加载

    /**
     * @ 文件名:   `07.延迟加载`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 15:03
     * @ 描述:    lateinit
     *
     * 1.by lazy 和lateinit  都可以单独使用或者放到成员变量中使用
     * 2.by lazy 知道具体值   用的时候再加载,lateinit  不知道具体值  后面再赋值
     * 3.by lazy变量必须通过val修饰  lateinit需要通过var修饰
     */
    fun main(args: Array<String>) {
        val user = User()
        println(user.name + user.phone)
    
        user.name = "林青霞"
        user.phone = "110"
    
        title = "标题"
    
        println(user.name + user.phone)
    }
    
    
    lateinit var title: String
    //lateinit var money: Byte //lateinit不支持原始数据类型
    
    class User {
        lateinit var name: String
        lateinit var phone: String
    }
    
  1. 扩展函数

    /**
     * @ 文件名:   `08.扩展函数`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 15:10
     * @ 描述:    String.myIsEmpty()
     *
     * 判断字符串是否为空
     */
    
    fun main(args: Array<String>) {
        val str = null
        //val str = "abc"
        val isEmpty = str?.myIsEmpty()
        println(isEmpty)
    
    }
    
    fun String.myIsEmpty(): Boolean {
        return this == null || this.isEmpty()
    }
    
  1. kotlin中的object单例

    /**
     * @ 文件名:   `09.kotlin中的object单例`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 15:48
     * @ 描述:    object ClassName
     */
    
    fun main(args: Array<String>) {
        println(SingleInstance.name)
        SingleInstance.login("110", "12345")
    }
    
    object SingleInstance {
        var name: String = "张三"//private static String name;
        fun login(phone: String, pwd: String) {
            println("登录成功: $phone + $pwd")
        }
    
        /* 实现单例的关键代码
        static {
            SingleInstance var0 = new SingleInstance();
            INSTANCE = var0;
            name = "张三";
        }*/
    }
    
  1. 伴生对象

    /**
     * @ 文件名:   `10.伴生对象`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:21
     * @ 描述:    companion object {  }
     */
    fun main(args: Array<String>) {
        val person = Person()
        println(person.age)
        println(Person.name)
    }
    
    class Person {
        //非静态属性
        var age = 10
    
        companion object {
            //静态属性
            var name = "林志玲"
        }
    }
    
  1. 和Java一样的单例

    /**
     * @ 文件名:   `11.和Java一样的单例`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:35
     * @ 描述:
     */
    fun main(args: Array<String>) {
        println(Utils.instance.isCanLogin("110"))
    }
    
    /**
     * 1,私有化构造方法:private constructor()
     * 2,初始化一个私有的静态成员变量:companion object { val instance}
     * 3,对外提供一个公共的方法返回该对象:by lazy { Utils() }
     */
    class Utils private constructor() {
    
        companion object {
            val instance by lazy { Utils() }//惰性加载,只会加载一次,并且线程安全的
        }
    
        fun isCanLogin(phone: String): Boolean {
            return phone.myIsEmpty() && phone.length == 11
        }
    }
    
  1. 枚举

    /**
     * @ 文件名:   `12.枚举`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:35
     * @ 描述:   定义一个枚举类,星期一到星期天 Monday Tuesday Wednesday Thursday Friday Saturday Sunday
     *          并定义一个方法判断是否是工作日
     */
    fun main(args: Array<String>) {
    
        println(todo(WEEK.TUESDAY))
        WEEK.values().forEach {
            println(it)
        }
    }
    
    enum class WEEK {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }
    
    fun todo(week: WEEK): String {
        return when (week) {
            in WEEK.MONDAY..WEEK.FRIDAY -> {
                "工作日"
            }
            WEEK.SATURDAY, WEEK.SUNDAY -> {
                "休息日"
            }
            else -> {
                "其他情况"
            }
        }
    }
    
  1. 枚举加强

    /**
     * @ 文件名:   `13.枚举加强`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:35
     * @ 描述:    定义一个枚举类 初始化三原色 R G B
     */
    fun main(args: Array<String>) {
        println(COLOR.RED.r)
        println(COLOR.GREEN.g)
        println(COLOR.BLUE.b)
    }
    
    enum class COLOR(var r: Int, var g: Int, var b: Int) {
        RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255)
    }
    
  1. 数据类

    /**
     * @ 文件名:   `14.数据类`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:35
     * @ 描述:    data class 定义一个JavaBean
     */
    
    fun main(args: Array<String>) {
        val banner = Banner("title", "http://www.baidu.com", 10011)
        println(banner.title + banner.url +
                banner.component3())//第三个元素 id
        val (title1, url, id) = Banner("title", "http://www.baidu.com", 10011)
        println(url)
    
    }
    
    // BaseBean abcd = new Gson().fromJson("json", BaseBean.class);
    
    data class Banner(
            var title: String,
            var url: String,
            var id: Int)
    
  1. 密封类

    /**
     * @ 文件名:   `15.密封类`
     * @ 创建者:   ty
     * @ 时间:    2018/7/20 16:49
     * @ 描述:    sealed class
     */
    fun main(args: Array<String>) {
    
        println(hasRightSon(NedStark.SansaStark()))
    
        println(hasRightSon(JohnSnow()))
    }
    
    fun hasRightSon(stark: NedStark):Boolean{
        return when (stark) {
            is NedStark.RobStark -> {
                true
            }
            is NedStark.SansaStark ->{
                true
            }
            is NedStark.AryaStark->{
                true
            }
            is NedStark.BrandonStark->{
                true
            }
            else -> {
                false
            }
        }
    }
    
    //密封类封装的是类型 类型是确定的
    sealed class NedStark {
        class RobStark : NedStark()
        class SansaStark : NedStark()
        class AryaStark : NedStark()
        class BrandonStark : NedStark()
    }
    
    class JohnSnow : NedStark()
    

推荐阅读更多精彩内容