3

你应该切换到Kotlin开发

 2 years ago
source link: http://www.androidchina.net/7918.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

原文地址:Why you should totally switch to Kotlin

现在是开始使用现在编程语言的时候了。

我想告诉你一个名为Kotlin的新编程语言并且阐述为什么你应该考虑下一个项目中使用它。我以前非常喜欢使用JAVA,但是去年当我自己使用了Kotlin的时候,我真的不会在去想Java是一个更好的选择(语言上)。

Kotlin是JetBrains公司开发的。JetBrains实际上是开发IDES。例如IntelliJReSharper。真正闪耀则是通过Kotlin,Kotlin是简洁和务实的,并且使得编码成为了令人满意和有效的体验。

尽管Kotlin可以编译JavaScript和机器码。但是我将会聚焦到最初的环境(JVM)

所以以下会给出几个你为什么需要切换到Kotlin语言开发的原因(没有先后顺序)

一、与Java的互通性

Kotlin能够达到和JAVA的100%互通,也就是说你可以使用Kotlin开发您的旧的JAVA项目.所有的你喜欢的Java框架都可以继续使用.而且你写的一些Kotlin的框架也都能非常容易的服务于你的java朋友爱好者.

二、熟悉的语法

Kotlin不是学术界(编程界)奇怪的(令人难以捉摸的语言).来自于OOP(面向对象编程语言)界的程序员都很熟悉.并且或多或少的都可以去理解.当然与Java还是有一定的区别的.比如说构造函数,或者是val,var变量声明.(下面的代码段则提供了一部分基础的说明)

class Foo {

    val b: String = "b"     // val means unmodifiable  不可以修改
    var i: Int = 0          // var means modifiable  可以修改

    fun hello() {
        val str = "Hello"
        print("$str World")
    }

    fun sum(x: Int, y: Int): Int {
        return x + y
    }

    fun maxOf(a: Float, b: Float) = if (a > b) a else b

}

三、字符串差值

就好像内置了一个更智能、更易读的Java版本的String.format().

val x = 4
val y = 7
print("sum of $x and $y is ${x + y}")  // sum of 4 and 7 is 11

四、类型推断

Kotlin会推断你的想要的类型(你将会觉得提高代码的阅读性)

val a = "abc"                         // type inferred to String
val b = 4                             // type inferred to Int

val c: Double = 0.7                   // type declared explicitly
val d: List<String> = ArrayList()     // type declared explicitly

五、聪明的类型转换

Kotlin编译器会跟踪您的逻辑做自动转换类型,这意味着不会有过多的instanceof检测(在明确的转换)

if (obj is String) {
    print(obj.toUpperCase())     // obj is now known to be a String
}

六、直观上的相等

你可以停止使用equals()方法来判断相等,因为== 这个操作符将会检测结构相等性。

val john1 = Person("John")
val john2 = Person("John")

john1 == john2    // true  (structural equality)
john1 === john2   // false (referential equality)

七、默认参数

不需要定义几个相似参数的方法

fun build(title: String, width: Int = 800, height: Int = 600) {
    Frame(title, width, height)
}

八、命名参数

结合默认参数,命名参数消除了构造者。

build("PacMan", 400, 300)                           // equivalent
build(title = "PacMan", width = 400, height = 300)  // equivalent
build(width = 400, height = 300, title = "PacMan")  // equivalent

九、when表达式

switch语句被替换成更加易读和灵活的when表达式

when (x) {
    1 -> print("x is 1")
    2 -> print("x is 2")
    3, 4 -> print("x is 3 or 4")
    in 5..10 -> print("x is 5, 6, 7, 8, 9, or 10")
    else -> print("x is out of range")
}

既可以作为一个表达式或者一个语句,也可以有参数或者没有参数

val res: Boolean = when {
    obj == null -> false
    obj is String -> true
    else -> throw IllegalStateException()
}

公开的参数自定义的set&get行为,这意味着可以阻止我们的代码膨胀(getters&setters带来的)

class Frame {
    var width: Int = 800
    var height: Int = 600

    val pixels: Int
        get() = width * height
}

十一、数据类(类似JavaBean)

一个普通的数据Bean(POJO)拥有toString(), equals(), hashCode(), and copy(),并且不会像Java一样超过100行代码

data class Person(val name: String,
                  var email: String,
                  var age: Int)

val john = Person("John", "[email protected]", 112)

十二、运算符重载

可以重载一组预定义的运算符以提高可读性

data class Vec(val x: Float, val y: Float) {
    operator fun plus(v: Vec) = Vec(x + v.x, y + v.y)
}

val v = Vec(2f, 3f) + Vec(4f, 1f)

十三、解构说明(Destructuring Declarations)

可以解构一些对象,这对于迭代数据集合的时候非常有用

for ((key, value) in map) {
    print("Key: $key")
    print("Value: $value")
}

十四、范围(Ranges)

方便可读性

for (i in 1..100) { ... } 
for (i in 0 until 100) { ... }
for (i in 2..10 step 2) { ... } 
for (i in 10 downTo 1) { ... } 
if (x in 1..10) { ... }

十五、扩展方法/功能

是否还记得第一次使用Java中List的排序么?你找不到一个sort方法进行排序从而你不得不咨询你的老师或者查找google来获取到Collections.sort()这个方法.后来当你使用一个String的时候可能你会写一个帮助类来帮助你达到想要的目的,因为你不知道有StringUtils.capitalize()

如果只有一种方法可以向旧类添加新功能,这样你的IDE将会帮助你在代码中找到相应的功能,这一点在Kotlin上面很容易实现.

fun String.format(): String {
    return this.replace(' ', '_')
}

val formatted = str.format()

标准库扩展了Java的原始类型的功能,这正是String特别需要的:

str.removeSuffix(".txt")
str.capitalize()
str.substringAfterLast("/")
str.replaceAfter(":", "classified")

十六、空值安全

Java是一种应该称之为几乎静态类型的语言,在Java中,String类型的变量不能保证引用String(安全) – 它可能引用为null,即使我们已经习惯了这个类型,它抵消了静态类型检查的安全性,所以Java开发人员将不会避免的遇见NPEs.(NullPointerException)或者想法设法的避免(有点多于的工作)
Kotlin则是通过区分非空类型和空类型来解决这个问题。默认情况下,类型为非空,并且可以默认添加一个?例如以下这样:

var a: String = "abc"
a = null                // compile error

var b: String? = "xyz"
b = null                // no problem

当你访问一个空的变量的时候,Kotlin会强制的避免NPES(NullPointerException)

val x = b.length        // compile error: b might be null

虽然这个看起来有点繁琐,但是因为他的这些功能真的可以是开发轻松起来,并且我们可以智能的转换,将空的类型转换为非空类型供我们使用.

if (b == null) return
val x = b.length        // no problem

并且我们可以安全的使用,当我们计算的为空的时候(而不是抛出空指针异常)

val x = b?.length       // type of x is nullable Int

安全调用可以链接在一起,以避免我们有时用其他语言编写的嵌套的if-not-null检查,如果我们想要一个非null之外的默认值,我们可以使用elvis操作符

val name = ship?.captain?.name ?: "unknown"

如果这样并不适用于你,而且你需要一个NPE,你将不得不明确地要求它

val x = b?.length ?: throw NullPointerException()  // same as below
val x = b!!.length                                 // same as above

十七、更好的Lambda表达式

好家伙,Kotlin更好的支持Lambda,基于他的聪明良好的语言设计,语法直接了当,可读性和简洁性更好.

val sum = { x: Int, y: Int -> x + y }   // type: (Int, Int) -> Int
val res = sum(4,7)                      // res == 11

更好的说明以下

  • 1、如果lambda表达式的是方法的最后一个参数或唯一的参数,则可以移动或省略方法括号
  • 2、如果我们选择不声明一个single-argument-lambda,它将被隐式声明为it
    由以上两点可以得到以下三行代码形式:
numbers.filter({ x -> x.isPrime() })
numbers.filter { x -> x.isPrime() }
numbers.filter { it.isPrime() }

而且这也允许我们写出更加简洁漂亮的代码:

persons
    .filter { it.age >= 18 }
    .sortedBy { it.name }
    .map { it.email }
    .forEach { print(it) }

Kotlin的lambda系统功能的完美结合使其成为DSL创建的理想选择,查看Anko的DSL示例,旨在服务Android开发.

verticalLayout {
    padding = dip(30)
    editText {
        hint = “Name”
        textSize = 24f
    }
    editText {
        hint = “Password”
        textSize = 24f
    }
    button(“Login”) {
        textSize = 26f
    }
}

十八、IDE支持

如果您打算开始使用Kotlin,您可以有多种选择,但是我强烈建议您使用与Kotlin捆绑在一起的IntelliJ软件.
举一个小的例子,当我第一次尝试从StackOverflow中复制粘贴一些Java代码时,这个东西就弹出来了.

IntelliJ will notice if you paste Java code into a Kotlin file
IntelliJ will notice if you paste Java code into a Kotlin file

好了就到这了,谢谢你的阅读,如果你还不相信或者不开始使用Kotlin语言的话,这里有一些更有说服力的文章供你参考。

作者:若兰明月
链接:http://www.jianshu.com/p/ec841df96f1e

转载请注明:Android开发中文站 » 你应该切换到Kotlin开发


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK