Kotlin学习_包、控制流、返回与跳转

Simplest Version

本文是学习Kotlin的包相关,控制流相关以及返回与跳转相关,与Java比较大的区别是Kotlin用when来替换掉Java的switch,写好简单,可以有各种类型值的判断,另外可以指定返回和跳转到指定标签的位置。

包(Package)

与Java一样,Kotlin的源文件同样以包声明开始的。

1
2
3
4
5
6
7
package foo.bar

fun baz() {}

class Goo {}

// ...

源文件的所有内容(如类和函数)都包含在声明的包中。
所以,在上面的例子中,baz()的全名是foo.bar.bazGoo的全名是foo.bar.Goo

如果未指定包,则此类文件的内容属于没有名称的“default”包。

导入(Imports)

Kotlin中除了模块中默认导入的包,每个文件都可以被导入。
如导入一个包里面的类文件

1
import foo.Bar // 导入foo包里面的Bar

如导入一个包里面的所有内容(包,类,对象,等等)

1
import foo.* // foo中的所有都可以使用

如果命名有冲突,可以用as关键字来重命名解决冲突

1
2
import foo.Bar // Bar可以使用
import bar.Bar as bBar // bar.Bar可以直接用bBar表示

与Java不同,Kotlin没有单独的“import static”语法;
所有这些声明都使用常规import关键字导入。

控制流(Control Flow)

Kotlin的控制流有if``when``for``while四种。

if表达式

在Kotlin中,if表达式表示返回一个值(true或false),Kotlin中没有三目运算符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//传统用法
var max = a
if (a < b)
max = b

//带 else
var max: Int
if (a > b)
max = a
else
max = b

//作为表达式
val max = if (a > b) a else b

if 分支可以作为块,最后一个表达是是该块的值

1
2
3
4
5
6
7
8
val max = if (a > b){
print("Choose a")
a
}
else{
print("Choose b")
b
}

如果使用if作为一个表达式,表达式需要有一个else分支。

1
2
val max = if (a > b) a // 这样写是错误的
val max = if (a > b) a else b // 这样才是正确的

when表达式

Kotlin中的when就类似与Java的switch,但是与switch不同

1
2
3
4
5
6
7
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // 默认
print("x is neither 1 nor 2")
}
}

在其它分支都不匹配的时候默认匹配 else 分支,如果没有把所有可能和分支条件列出来,那么else是强制的,这与switchdefault也有区别。

分支条件可以连在以前判断

1
2
3
4
when (x) {
0,1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}

分支的判断条件可以用任意表达式

1
2
3
4
when (x) {
parseInt(s) -> print("s encode x")
else -> print("s does not encode x")
}

也可以用 in 或者 !in 检查值是否值在一个集合中

1
2
3
4
5
6
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}

也可以用 is 或者 !is 来判断值是否是某个类型,由于Kotlin可以自动判断变量的类型,所有在满足条件后的操作可以不用检查就能使用相应的属性或方法。

1
2
3
4
val hasPrefix = when (x) {
is String -> x.startsWith("prefix")
else -> false
}

when甚至可以用来替换if-else if,将when设置无参数提供就是简单的if表达式了

1
2
3
4
5
when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}

用了when之后,Java的switch简直就是辣鸡。。

for循环

for循环的内容可以是一个语句块

1
2
3
for (item: Int in ints) {
// ...
}

for 可以对任何提供的迭代器进行迭代,例如

  • has a member- or extension-function iterator(), whose return type
  • has a member- or extension-function next(), and
  • has a member- or extension-function hasNext() that returns Boolean.

如果需要使用list或者array的索引进行迭代,需这样写

1
2
3
for (i in array.indices) {
print(array[i])
}

while循环

whiledo...while 与Java的一样,有一个区别是,语句块里面的变量在外面是可见的

1
2
3
4
5
6
7
while (x > 0) {
x--
}

do {
val y = retrieveData()
} while (y != null) // y 在这是可见的

返回与跳转(Returns and Jumps)

Kotlin支持三种跳转操作符

  • return,结束最近的闭合循环
  • break,跳出最近的闭合循环
  • continue,跳到最近的闭合循环的下一次循环

break和continue标签(Break and Continue Labels)

Kotlin中的任何表达式都可以用标签标记,标签是后面加@符号的标识符的形式,例如:abc @fooBar @

1
2
3
loop@ for (i in 1..100){ // loop@就是标签
//...
}

使用break 跳转到标签处,跳出循环

1
2
3
4
5
6
7
loop@ for (i in 1..10) {
for (j in i..10) {
if (j == 5)
break@loop // 跳出循环
Log.e(Tag, j.toString()) // j 为5的时候跳出了循环,只打印1、2、3、4
}
}

使用continue跳转到标签处,进行下一次循环

1
2
3
4
5
6
7
loop@ for (i in 1..10) {
for (j in i..10) {
if (j == 5)
continue@loop // 跳出本次循环,进行下一次循环
Log.e(Tag, j.toString()) // j 为5的时候跳出了循环,所有不会打印5
}
}

return标签(Return at Labels)

在字面函数,局部函数,以及对象表达式中,函数可以在 Kotlin 中被包裹。return允许返回到外层函数。

1
2
3
4
5
6
fun foo() {
ints.forEach {
if (it == 0) return // 跳出forEach
print(it)
}
}

return表达式返回到最近的闭合函数,比如 foo,如果返回从一个函数返回可以使用return标签

1
2
3
4
5
6
7
fun foo() {
ints.forEach lit@ {
it (it ==0) return@lit
// 这样就不会return到foo函数,而是return到标签处的forEach函数
print(it)
}
}

通常这种情况用一种更方便的标签,例如用一个和传入的 Lambda 表达式名字相同的标签。

1
2
3
4
5
6
7
fun foo() {
ints.forEach {
if (it ==0) return@forEach
// 与上面一样,return到标签处的forEach函数
print(it)
}
}

另外,可以用匿名函数替换lambda表达式,使用 return 语句可以从匿名函数中返回

1
2
3
4
5
6
fun foo() {
ints.forEach(fun(value: Int){
if (value == 0) return
print(value)
})
}

当返回一个值的时候,如return@a 1,表示在标签@a处返回1,而不是返回带标签的表达式@a 1