V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
liusd
V2EX  ›  Kotlin

我与 Kotlin 的爱恨情仇之浅谈 Extensions

  •  
  •   liusd · 2017-05-25 15:39:05 +08:00 · 4347 次点击
    这是一个创建于 2520 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Kotlin, similar to C# and Gosu, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions. Kotlin supports extension functions and extension properties.

    上一个章节《我与 Kotlin 的爱恨情仇之浅谈 Type aliases 》中,带领大家一起领略了一下 typealias的基本用法。可以让你写出更加优美的代码。今天再带大家一起来领略一下 Kotlin 中另一个神奇好玩的东西: Extensions.

    Extensions 是什么?

    千万不要被 JAVA 中的 extend 给带歪了的,一个是扩展,一个是继承,完全不是一码事。而且这也是我讨厌的一点,至于原因我接下来就会讲到,当然纯属我个人 认知喜好问题。

    文章口头就引用了《 reference/extensions 》中的一段话,已经解释了 Kotlin 具备可以 extension (扩展) functions 以及 properties 的能力。

    我爱 Extensions

    • Extension Functions 我们可以给任何一个 包括系统 的类追加扩展方法

      MutableList 扩展(追加) 一个 swap 方法,用于交互两个 indexvalue :

      fun MutableList<Int>.swap(index1: Int, index2: Int) {
          val tmp = this[index1] // 'this' corresponds to the list
          this[index1] = this[index2]
          this[index2] = tmp
      }   
      
      

      具体用的时候:

      
      val l = mutableListOf(1, 2, 3)
      l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'
      
      
    • Extension Properties 我们可以给任何一个 包括系统 的类追加属性,但切记属性必须 按约定来

      我们可以给给 List 追加一个 lastIndex 属性

      val <T> List<T>.lastIndex: Int
      get() = size - 1
      

      但是我说的约束是不能这样子写

       val Foo.bar = 1 // error: initializers are not allowed for extension properties
      

      更多注意事项请看 《官方文档》

      我们项目中拿他来干什么呢?

    Google 一搜索一大把,比如出了名的 anko ,里面提供大量的 Extension。 但我并没有用过 anko,虽然使用 KotlinAndroid 项目已经超过两年了的,原因是个人觉得太重了的,我们自己也封装了一个轻量级的 AndroidExtension

    比如万恶的 findViewById,可以写成这样:

           val textview = findview(R.id.txt)
    

    比如臭长的 Toast, 现在也可以简单到这样啦:

          toast("hello,I'M SK")
    

    还有更多有意思的,可以大大提高开发效率的扩展,具体可以查看我们的 KotlinThree GitHub

    我不爱 Extensions

    no, no, no 不能不爱,用起来实在是太爽了的,极大的提高了我的日常开发效率,但非要让我挑毛病的话,我只能说因为当初的 Swift 坑了我吧,我第一反应是跟 Swift extension 一样,终于可以摆脱之前要在一个类的开头,写一堆的接口,类似这样:

    image.png 但在 Swift 中可以用 extension 完美解决这个问题: 好吧,所以在没有看仔细看 Kotlin 文档的时候,我以为 extension 用法和 Swift 一样呢,但完全不是一个概念。Kotlin 中压根就没有这个 extension 关键字。

    写在最后

    这个语法糖,你可以随便去扩展你自己想要的功能,从而极大的提高我们开发效率,用起来确实让人爱不释手。那么 Kotlin 内部是怎么实现的? 可以看看编译出来的文件,其实是帮忙做了一层转换,把我们的扩展的方法做成一个 static method, 然后调用的地方其实是调用生成的的这个 static method,之前《我与 Kotlin 的爱恨情仇之浅谈 block 》谈到的另一把我爱不释手的利器 block 在评论中我也说了的,都是人家最后帮我们去生成了代码,然后 一样的做法,虽然效率并没有任何提升,有兴趣的话,您 不妨自己反编译试试。但却不 得不说,大大提高了我们本身编码效率,毕竟重复的代码,谁写着都觉得心累。

    查看原文

    4 条回复    2017-05-25 21:55:10 +08:00
    ywu
        1
    ywu  
       2017-05-25 15:52:12 +08:00
    就是 mixin 吧
    liusd
        2
    liusd  
    OP
       2017-05-25 15:53:53 +08:00
    @ywu mixin???
    dhssingle
        3
    dhssingle  
       2017-05-25 21:20:36 +08:00 via iPhone
    这不就是 c#的扩展方法
    liusd
        4
    liusd  
    OP
       2017-05-25 21:55:10 +08:00
    @dhssingle 是的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5206 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:28 · PVG 17:28 · LAX 02:28 · JFK 05:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.