前言
本文章只是用于记录学习,所以部分地方如果有错误或者理解不对的地方,麻烦请指正。
1.枚举
enum class color{
RED,
GREEN,
BLUE
}
在kotlin 中声明枚举类需要添加 enum class 枚举名,enum 算是一个 软关键词,所以不需搭配 class ,和java 中 枚举类是值的列表 不一样,kotiln 是可以给枚举添加 属性和方法的。如下段代码,
enum class color(val r:Int , val g:Int ,val b:Int){
RED(255,0,0),
GREEN(0,255,0),
BLUE(0,0,255);
fun rgb(){
println(r+g+b)
}
}
// main 方法中引用
color.BLUE.rgb()
上图 枚举类的构造方法和属性的时候和常规类 是一样的,所以声明 枚举类常亮的时候就必须输入属性值,上段代码 也是 介绍到了 kotlin 中少数需要 分开的地方,用于将 常亮列表 和 方法 的分开。
2.when循环
结合上边我们学到的 枚举 我们结合when 学习一下基础使用
fun getColor(color :Color){
when(color){
Color.BLUE -> println("this is blue")
Color.RED -> println("this is RED")
Color.GREEN -> println("this is GREEN")
}
}
// main 调用
getColor(Color.GREEN)
如上段代码,我们使用when 来判断,但是 我们不用每一句后边都添加 break 来代表结束,kotlin 中如果 某一个分支 匹配成功,就不会进入到其他的分支,当然也可以 多条件,如下
fun getColor(color :Color){
when(color){
Color.BLUE ,Color.RED -> println("this is blue or red")
Color.GREEN -> println("this is GREEN")
}
}
如果有 两到多个条件,条件之间可以用 逗号分隔。当然kotlin中也可以使用 任意对象对接使用
fun maxColor(color1 :Color ,color2 :Color){
when(setOf(color1 ,color2)){
setOf(Color.BLUE ,Color.RED) -> println("this is blue or red")
setOf(Color.BLUE ,Color.GREEN) -> println("this is GREEN")
else -> println("this is not max color")
}
}
when 检查 分支是否符合条件,如果都不符合就走 else ,当然这样比较每次都会创建 一些set 对象,我们也可以 when中 不添加参数,直接比较 color1 和 color 2 是否一样,但是代码更多,有一点不太好理解。
3.if
在上一张主要用到了 if 和 if的缩写形式,如果if 返回单句表达式。
fun max(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
// 单句表达式
fun getResult(a: Int, b: Int) = a + b
// 加上 if
fun getMaxResult(a: Int, b: Int) = if(a>b) a else b
4. 空检测和类型检测
kotlin 中如果 数值 或 返回值可以为 空的时候 则可以在 类型后边加上 ?
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// `obj`在这个分支中自动转换为`String`类型
return obj.length
}
// `obj`仍然是`Any`类型
return null
}
fun getStringLength(obj: Any): Int? {
// `obj`在这个分支中自动转换为`String`类型
if (obj !is String) return null
return obj.length
}
该方法返回值可以为 null 也可以是 int 类型 字符串的长度,在判断中我们有看到了新的 词语 is ,is 表达式主要检查 表达式或者值 的类型是否是 is 后边的类型,如果是 则会进行自动转换,如果不是则 仍然保持原来的数据类型。
上段代码中我们可以看到 kotlin 和 java 的类型转换的差别,java 使用instanceOf 来判断类型后,如果要转型需要 显式将该类型转换为我们比较的类型,而 kotlin 中 使用 is 检查过 数据后,如果类型正确 则会 智能转型,这一步是编译器自动帮我们完成的。
当然如果我们需要使用 显式转换的时候 可以使用 as 表达式,(val n = A as Num)
5.for 及 in
kotlin 中的for 循环相较于 java 更加的简洁
// in操作符可以判断是否arg是否在args里面
for (arg in args) {
print(arg)
}
// 另外的一种姿势
for (i in args.indices) {
print(args[i])
}
上段代码中我们使用了 in ,接下在 我们主要看下 in 的搭配使用和每种使用标识的具体意思
if (x in 1..y-1) { // x是否在 1到y-1的范围
print("OK")
}
for (i in 1..100) { ... } // 1到100范围
for (i in 1 until 100) { ... } // 半开范围,不包括100,相当于[1,100)
for (x in 2..10 step 2) { ... } // 每次加2,内容为 x , x+2 ,x+4 ...
for (x in 10 downTo 1) { ... } // 倒序
如果让我们用 kotlin 写 1 到100 倒序的所有 偶数,我们就可以这样写
for(i in 100 downTo 1 step 2){
prinln(....)
}
6. while
kotlin 中的 while 和java 是类似的
while (i < args.size) {
print(args[i++])
}
7. 异常
kotlin 中的 异常 和 java 中的异常是差不多的,不过java 中需要new ,而kotlin中 都不需要new,直接 throw IllegalArgumentException(“err”) , kotlin 中的 throw 算是一种表达式,可以在另外的表达式中使用。
fun maxColor(color1 :Color ,color2 :Color){
when(setOf(color1 ,color2)){
setOf(Color.BLUE ,Color.RED) -> println("this is blue or red")
setOf(Color.BLUE ,Color.GREEN) -> println("this is GREEN")
else -> throw IllegalArgumentException("err")
}
}
java 和 kotlin 中的 try catch finally 适用方式是一样的,不过在kotlin中,try catch 为一种表达式。
fun maxColor(color1 :Color ,color2 :Color) {
var str = try {
when(setOf(color1 ,color2)){
setOf(Color.BLUE ,Color.RED) -> "this is blue or red"
setOf(Color.BLUE ,Color.GREEN) -> "this is GREEN"
else -> throw IllegalArgumentException("err")
}
} catch (e:IndexOutOfBoundsException){
null
}
print(str)
}
从上边代码可以看到,try 作为 表达式 可以将值赋值给一个变量的,如果when 比较分支 成功 就会打印对应的 字符串,如果都没有就抛出异常,catch 捕捉异常,返回 null。