美文网首页KotlinKotlinKotlin专题
Kotlin-19.代理/委托类(Delegation)

Kotlin-19.代理/委托类(Delegation)

作者: lioilwin | 来源:发表于2017-08-07 21:24 被阅读49次

    官方文档: http://kotlinlang.org/docs/reference/delegation.html

    类代理/委托(Class Delegation)

    代理/委托模式(Delegation pattern)已被证明是替代继承的一个很好方式,
    而Kotlin原生支持它:    
        interface Base {
            fun p1()
            fun p2()
        }
    
        class Impl() : Base {
            override fun p1() {
                println("p1_Impl")
            }
            override fun p2() {
                println("p2_Impl")
            }
        }
    
        //by base表示 Deg会存储base,代理Base所有方法/函数
        class Deg(base: Base) : Base by base{
            override fun p2() {
                println("p2_Deg")
            }
        }
    
        fun main(args: Array<String>) {
            val deg = Deg(Impl())
            deg.p1() //输出p1_Impl
            deg.p2() //输出p2_Deg
        }
    

    简书:http://www.jianshu.com/p/75177a9e91b7
    CSDN博客: http://blog.csdn.net/qq_32115439/article/details/73718749
    GitHub博客:http://lioil.win/2017/06/25/Kotlin-delegation.html
    Coding博客:http://c.lioil.win/2017/06/25/Kotlin-delegation.html

    相关文章

      网友评论

        本文标题:Kotlin-19.代理/委托类(Delegation)

        本文链接:https://www.haomeiwen.com/subject/fpfzlxtx.html