[关闭]
@cxm-2016 2016-11-21T02:11:09.000000Z 字数 3111 阅读 3145

Kotlin(二)——惯用法

Kotlin

版本:2
翻译:李颖

转载自:Kotlin Reference



本章介绍 Kotlin 中的一些常见的习惯用法. 如果你有自己的好的经验, 可以将它贡献给我们. 你可以将你的修正提交到 git, 并发起一个 Pull Request.

创建 DTO 类(或者叫 POJO/POCO 类)

  1. data class Customer(val name: String, val email: String)

以上代码将创建一个 Customer 类, 其中包含以下功能:

对函数参数指定默认值

  1. fun foo(a: Int = 0, b: String = "") { ... }

过滤 List 中的元素

  1. val positives = list.filter { x -> x > 0 }

甚至还可以写得更短:

  1. val positives = list.filter { it > 0 }

在字符串内插入变量值

  1. println("Name $name")

类型实例检查

  1. when (x) {
  2. is Foo -> ...
  3. is Bar -> ...
  4. else -> ...
  5. }

使用成对变量来遍历 Map, 这种语法也可以用来遍历 Pair 组成的 List

  1. for ((k, v) in map) {
  2. println("$k -> $v")
  3. }

上例中的 k, v 可以使用任何的变量名.

使用数值范围

  1. for (i in 1..100) { ... } // 闭区间: 包括 100
  2. for (i in 1 until 100) { ... } // 半开(half-open)区间: 不包括 100
  3. for (x in 2..10 step 2) { ... }
  4. for (x in 10 downTo 1) { ... }
  5. if (x in 1..10) { ... }

只读 List

  1. val list = listOf("a", "b", "c")

只读 Map

  1. val map = mapOf("a" to 1, "b" to 2, "c" to 3)

访问 Map

  1. println(map["key"])
  2. map["key"] = value

延迟计算(Lazy)属性

  1. val p: String by lazy {
  2. // compute the string
  3. }

扩展函数

  1. fun String.spaceToCamelCase() { ... }
  2. "Convert this to camelcase".spaceToCamelCase()

创建单例(Singleton)

  1. object Resource {
  2. val name = "Name"
  3. }

If not null 的简写表达方式

  1. val files = File("Test").listFiles()
  2. println(files?.size)

If not null … else 的简写表达方式

  1. val files = File("Test").listFiles()
  2. println(files?.size ?: "empty")

当值为 null 时, 执行某个语句

  1. val data = ...
  2. val email = data["email"] ?: throw IllegalStateException("Email is missing!")

当值不为 null 时, 执行某个语句

  1. val data = ...
  2. data?.let {
  3. ... // 这个代码段将在 data 不为 null 时执行
  4. }

在函数的 return 语句中使用 when 语句

  1. fun transform(color: String): Int {
  2. return when (color) {
  3. "Red" -> 0
  4. "Green" -> 1
  5. "Blue" -> 2
  6. else -> throw IllegalArgumentException("Invalid color param value")
  7. }
  8. }

将 ‘try/catch’ 用作一个表达式

  1. fun test() {
  2. val result = try {
  3. count()
  4. } catch (e: ArithmeticException) {
  5. throw IllegalStateException(e)
  6. }
  7. // 使用 result
  8. }

将 ‘if’ 用作一个表达式

  1. fun foo(param: Int) {
  2. val result = if (param == 1) {
  3. "one"
  4. } else if (param == 2) {
  5. "two"
  6. } else {
  7. "three"
  8. }
  9. }

返回值为 Unit 类型的多个方法, 可以通过 Builder 风格的方式来串联调用

  1. fun arrayOfMinusOnes(size: Int): IntArray {
  2. return IntArray(size).apply { fill(-1) }
  3. }

使用单个表达式来定义一个函数

  1. fun theAnswer() = 42

以上代码等价于:

  1. fun theAnswer(): Int {
  2. return 42
  3. }

这种用法与其他惯用法有效地结合起来, 可以编写出更简短的代码. 比如. 可以与 when-表达式结合起来:

  1. fun transform(color: String): Int = when (color) {
  2. "Red" -> 0
  3. "Green" -> 1
  4. "Blue" -> 2
  5. else -> throw IllegalArgumentException("Invalid color param value")
  6. }

在同一个对象实例上调用多个方法(‘with’ 语句)

  1. class Turtle {
  2. fun penDown()
  3. fun penUp()
  4. fun turn(degrees: Double)
  5. fun forward(pixels: Double)
  6. }
  7. val myTurtle = Turtle()
  8. with(myTurtle) { // 描绘一个边长 100 像素的正方形
  9. penDown()
  10. for(i in 1..4) {
  11. forward(100.0)
  12. turn(90.0)
  13. }
  14. penUp()
  15. }

类似 Java 7 中针对资源的 try 语句

  1. val stream = Files.newInputStream(Paths.get("/some/file.txt"))
  2. stream.buffered().reader().use { reader ->
  3. println(reader.readText())
  4. }

对于需要泛型类型信息的函数, 可以使用这样的简便形式

  1. // public final class Gson {
  2. // ...
  3. // public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
  4. // ...
  5. inline fun <reified T: Any> Gson.fromJson(json): T = this.fromJson(json, T::class.java)

使用可为 null 的布尔值

  1. val b: Boolean? = ...
  2. if (b == true) {
  3. ...
  4. } else {
  5. // `b` 为 false 或为 null
  6. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注