发布于 2017-06-05 00:55:49 | 218 次阅读 | 评论: 3 | 来源: 网友投递
Kotlin 基于JVM的编程语言
Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发。
我想告诉你一个名为 Kotlin 的新的编程语言,以及为什么你要开始考虑使用它来开发你的下一个项目。我以前喜欢 Java ,但是去年我发现了 Kotlin ,只要有可能我就会用 Kotlin 来写代码。现在我实在无法想象有什么地方只有 Java 能做,而 Kotlin 不能的。
Kotlin 是 JetBrains 开发的,这是一家开发了一整套 IDEs 的公司,诸如 IntelliJ 和 ReSharper, 还有正在闪耀光芒的 Kotlin。这是一个务实而且简洁的编程语言,真正让人感觉身心愉悦的编程语言,而且效率非常高。
这里有一堆你应该完全转向 Kotlin 语言的理由:
Kotlin 是 100% 与 Java 具备互操作性的。你可以使用 Kotlin 继续你老的 Java 项目开发。所有你熟悉的 Java 框架仍然可用,任何框架,不管是 Kotlin 写的,还是你固执 Java 小伙伴写的都可以。
Kotlin 不是诞生于学术界的怪异语言。其语法是 OOP 领域的任何开发者都熟悉的,可以或多或少的有一些了解。当然和 Java 还是有些不同的,例如重构后的构造函数以及使用 val 的变量声明等等。下面是一个基本的 Kotlin 示例代码:
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() 方法的 Kotlin 实现:
val x = 4val y = 7print("sum of $x and $y is ${x + y}") // sum of 4 and 7 is 11
Kotlin 会自动的对变量的类型进行推断:
val a = "abc" // type inferred to Stringval b = 4 // type inferred to Intval c: Double = 0.7 // type declared explicitlyval 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)
不需要像 Java 那样定义很多包含不同参数的相似方法:
fun build(title: String, width: Int = 800, height: Int = 600) { Frame(title, width, height) }
结合默认参数值,命名参数可以消除 builders 的使用:
build("PacMan", 400, 300) // equivalent build(title = "PacMan", width = 400, height = 300) // equivalent build(width = 400, height = 300, title = "PacMan") // equivalent
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 }
这是一个 POJO 类,包含 toString()
, equals()
, hashCode()
, 和 copy() 方法,和 Java 不同的是,它不会超过 100 行代码:
data class Person(val name: String, var email: String, var age: Int)val john = Person("John", "john@gmail.com", 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)
一些对象是可以解构的,一个很有用的例子就是对 Map 进行迭代:
for ((key, value) in map) { print("Key: $key") print("Value: $value") }
完全为了可读性:
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() 函数,然后你就四处求助,最终找到了 Collections.sort()。后来你需要将一个字符串的首字符变成大写,最终你还是自己写了一个方法来实现,因为你当时还不知道有这么个东西
StringUtils.capitalize()
.
如果只有一种方法可以向已有的类添加新的函数,这样 IDE 就可以帮助你在代码完成时找到正确的函数。在 Kotlin 里你可以这么做:
fun String.replaceSpaces(): String { return this.replace(' ', '_') }val formatted = str.replaceSpaces()
标准库扩展了 Java 原来类型的功能,这是字符串对象所需要的:
str.removeSuffix(".txt") str.capitalize() str.substringAfterLast("/") str.replaceAfter(":", "classified")
Java 是我们应该称之为是一个几乎静态类型的编程语言。Java 的 String 变量类型无法保证它不会等于 null。尽管我们已经习惯了这样的情况,但它否定了静态类型检查的安全性,导致 Java 程序员总是活在各种空指针异常的恐惧中。
Kotlin 通过区分非空类型和允许为空类型来解决这个问题。类型默认是不允许为空的,可以通过在后面加一个 ? 问号来表示允许为空,例如:
var a: String = "abc" a = null // 编译错误var b: String? = "xyz" b = null // 正确
Kotlin 强制你必须在访问一个可为空的类型变量时候必须确保不会发生空指针:
val x = b.length // 编译错误: b 允许为空
虽然看起来有点麻烦,但这的确是 Kotlin 一个微小却又非常重要的特性。我们仍可以使用智能的转换,可以在需要的时候将允许为空的类型转成不允许为空:
if (b == null) returnval x = b.length // 正确
我们也可以使用安全调用方法 ?. 该表达式在 b 为空时返回 null,而不是抛出空指针异常:
val x = b?.length // type of x is nullable Int
安全调用可以链接在一起,以避免像其他语言一样存在大量嵌套的 if-not-null 检查,如果我们想要一个默认值而不是 null 时,可以用 ?: 操作符:
val name = ship?.captain?.name ?: "unknown"
如果没有适合你的,那么你应该是需要一个空指针了,你将不得不显式的进行处理:
val x = b?.length ?: throw NullPointerException() // same as belowval x = b!!.length // same as above
嘿,帅哥,这是一个很好的 Lambda 系统 —— 在完美的可读性和简洁之间取得非常好的平衡,这得益于非常聪明的设计选择。其语法简单直接:
val sum = { x: Int, y: Int -> x + y } // type: (Int, Int) -> Intval res = sum(4,7) // res == 11
优点体现在:
如果 lambda 是方法的最后一个参数或者是唯一参数的话,那么方法的括号可以移动或者省略.
如果我们选择不去声明单参数的 lambda 表达式的参数,那么 Kotlin 会隐式的将之命名为 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 } }
如果你打算开始使用 Kotlin 开发项目,在工具上你会有很多的选择。但我强烈推荐使用 IntelliJ ,因为它自带 Kotlin 的支持 — 它的特点展示了同一组人设计的开发工具和语言时的优势。
只是给你一个很小但有非常聪明的例子,这是我从 Stackoverflow 粘贴过来的 Java 代码执行后弹出的窗口:
IntelliJ 会提醒你粘贴了 Java 的代码到 Kotlin 文件里。
译文来源:可译网 译者:CY2
英文原文:Why you should totally switch to Kotlin
I've reached out several times but haven't heard back, which tells me one of three things:
1. You’re interested in giving link back, but haven't had a chance to get back to me yet.
2. You're not interested and want me to stop emailing.
3. You've fallen and can't get up – in that case let me know and I'll call 911.
Can you please reply with 1, 2 or 3? I don't want to be a bother.
==================== Original message =====================
Hi...ha just kidding :)
Hi
You probably don't remember me..mainly because we've never met. i'm Alex, an Editor at Guru99.
^ This is me.
While this email is going to be me asking for a backlink, i'm doing this with a twist...
I loved reading at your http://phperz.com/article/17/0605/335945.html. Eventually i noticed that you shared https://kotlinlang.org/docs/reference/control-flow.html.
And i'd love to know if you drop us a mention on it for our https://www.guru99.com/kotlin-tutorial.html
Why is our article great? Good question..
Our article contains everything readers need to know. I hope your audience finds this a valuable-add on :)
What's in it for me? You're 2/2!
I can help you with any of the following (pick wisely):
• Social sharing of your article to our 50k social subscribers
• You will update the content and we all know Google likes that
• You will make me extremely happy
Above all, hope you're staying positive and testing negative.
regards,
Alex.
PS: I am real a person... here is a number to contact me +1 302 308 5151
I am curious to know your thoughts on my proposal below.
I welcome your feedback or rejection ( I pray not : - ) )
==================== Original message =====================
Hi...ha just kidding :)
Hi
You probably don't remember me..mainly because we've never met. i'm Alex, an Editor at Guru99.
^ This is me.
While this email is going to be me asking for a backlink, i'm doing this with a twist...
I loved reading at your http://phperz.com/article/17/0605/335945.html. Eventually i noticed that you shared https://kotlinlang.org/docs/reference/control-flow.html.
And i'd love to know if you drop us a mention on it for our https://www.guru99.com/kotlin-tutorial.html
Why is our article great? Good question..
Our article contains everything readers need to know. I hope your audience finds this a valuable-add on :)
What's in it for me? You're 2/2!
I can help you with any of the following (pick wisely):
• Social sharing of your article to our 50k social subscribers
• You will update the content and we all know Google likes that
• You will make me extremely happy
Above all, hope you're staying positive and testing negative.
regards,
Alex.
PS: I am real a person... here is a number to contact me +1 302 308 5151
Hi
You probably don't remember me..mainly because we've never met. i'm Alex, an Editor at Guru99.
^ This is me.
While this email is going to be me asking for a backlink, i'm doing this with a twist...
I loved reading at your http://phperz.com/article/17/0605/335945.html. Eventually i noticed that you shared https://kotlinlang.org/docs/reference/control-flow.html.
And i'd love to know if you drop us a mention on it for our https://www.guru99.com/kotlin-tutorial.html
Why is our article great? Good question..
Our article contains everything readers need to know. I hope your audience finds this a valuable-add on :)
What's in it for me? You're 2/2!
I can help you with any of the following (pick wisely):
• Social sharing of your article to our 50k social subscribers
• You will update the content and we all know Google likes that
• You will make me extremely happy
Above all, hope you're staying positive and testing negative.
regards,
Alex.
PS: I am real a person... here is a number to contact me +1 302 308 5151