Kotlin基础语法
在 Kotlin 中,所有东西都是对象
注释
与大多数现代语言一样,Kotlin 支持单行(或行末)与多行(块)注释。
// 这是单行注释
/* 这是多行
块注释。 */
// Kotlin 中的块注释可以嵌套
/* 注释从这里开始
/* 包含嵌套的注释 */
并且在这里结束。 */
类型声明
- 使用关键字val 定义只读局部变量,只能为其赋值一次
val a: Int = 1 // 立即赋值
val b = 2 // 自动推断出 `Int` 类型
val c: Int // 如果没有初始值类型不能省略
c = 3 // 明确赋值
- 使用 var 关键字, 可重新赋值的变量
// 自动推断出 `Int` 类型
var x = 5
x += 1
字符串模板
var a = 1
// 模板中的简单名称:
val s1 = "a is $a"
a = 2
// 模板中的任意表达式:
val s2 = "${s1.replace("is", "was")}, but now is $a"
声明函数返回值类型
- 使用关键字fun定义sum函数,两个Int参数,返回Int函数
fun sum(x:Int, y:Int): Int {return x+y}
- 将表达式作为函数体、返回值类型自动推断的函数
fun sum(a: Int, b: Int) = a + b
- 函数返回无意义的值
fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}")
}
Unit 返回类型可以省略
fun printSum(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}")
}
条件表达式
fun maxof(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
也可以这么写
fun maxOf(a: Int, b: Int) = if (a > b) a else b
空值与 null 检测
当某个变量的值可以为 null 的时候,必须在声明处的类型后添加?来标识该引用可为空。
// 如果 str 的内容不是数字返回 null
fun parseInt(str: String): Int? {
// ……
}
// 使用返回可空值的函数
fun printProduct(arg1: String, arg2: String) {
val x = parseInt(arg1)
val y = parseInt(arg2)
// 直接使用 `x * y` 会导致编译错误,因为它们可能为 null
if (x != null && y != null) {
// 在空检测后,x 与 y 会自动转换为非空值(non-nullable)
println(x * y)
}
else {
println("'$arg1' or '$arg2' is not a number")
}
}
// 或者这么使用
if (x == null) {
println("Wrong number format in arg1: '$arg1'")
return
}
if (y == null) {
println("Wrong number format in arg2: '$arg2'")
return
}
// 在空检测后,x 与 y 会自动转换为非空值
println(x * y)
类型检测与自动类型转换
is 运算符检测一个表达式是否某类型的一个实例。 如果一个不可变的局部变量或属性已经判断出为某类型,那么检测后的分支中可以直接当作该类型使用,无需显式转换。
// eg1
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// `obj` 在该条件分支内自动转换成 `String`
return obj.length
}
// 在离开类型检测分支后,`obj` 仍然是 `Any` 类型
return null
}
// eg2
fun getStringLength(obj: Any): Int? {
if (obj !is String) return null
// `obj` 在这一分支自动转换为 `String`
return obj.length
}
// eg3
fun getStringLength(obj: Any): Int? {
// `obj` 在 `&&` 右边自动转换成 `String` 类型
if (obj is String && obj.length > 0) {
return obj.length
}
return null
}
循环
- for循环
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
}
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
- while循环
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
- when表达式
fun describe(obj: Any): String =
when (obj) {
1 -> "One"
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
else -> "Unknown"
}
- 使用区间(range)
使用 in 运算符来检测某个数字是否在指定区间内
val x = 10
val y = 9
if (x in 1..y+1) {
println("fits in range")
}
检测某个数字是否在指定区间外
val list = listOf("a", "b", "c")
if (-1 !in 0..list.lastIndex) {
println("-1 is out of range")
}
if (list.size !in list.indices) {
println("list size is out of valid list indices range, too")
}
区间迭代
for (x in 1..5) {
print(x)
}
数列迭代
for (x in 1..10 step 2) {
print(x)
}
println()
for (x in 9 downTo 0 step 3) {
print(x)
}
集合
- 对集合进行迭代
for (item in items) {
println(item)
}
- 使用 in 运算符来判断集合内是否包含某实例
when {
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
}
- 使用 lambda 表达式来过滤(filter)与映射(map)集合
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
fruits
.filter { it.startsWith("a") }
.sortedBy { it }
.map { it.toUpperCase() }
.forEach { println(it) }
创建基本类及其实例
val rectangle = Rectangle(5.0, 2.0)
val triangle = Triangle(3.0, 4.0, 5.0)
- 类的修饰符
// 类属性修饰符
abstract // 抽象类
final // 类不可继承,默认属性
enum // 枚举类
open // 类可继承,类默认是final的
annotation // 注解类
// 访问权限修饰符
private // 仅在同一个文件中可见
protected // 同一个文件中或子类可见
public // 所有调用的地方都可见
internal // 同一个模块中可见
- 内部类与嵌套类
- 构造器
在kotlin中有主,次两种构造器,写在类名后面的是主构造器,在类中的是次构造器,次构造器必须调用主构造器
,一个类最多只能有1个主构造器(也可以没有),而次构造器是没有个数限制。
主构造器中的参数在类的属性中使用,也可以在init代码块中使用。
class User constructor(var name: String) {
init{
this.name = name
}
constructor(name: String, id: Int) :this(name){
}
}
主构造器里直接声明属性
class User(var name: String) {
}
// 等价于:
class User(name: String) {
var name: String = name
}
- 嵌套类
class Outer { // 外部类
private val bar: Int = 1
class Nested { // 嵌套类
fun foo() = 2
}
}
fun main(args: Array<String>) {
val demo = Outer.Nested().foo() // 调用格式:外部类.嵌套类.嵌套类方法/属性
println(demo) // == 2
}
- 内部类
内部类使用 inner 关键字来表示。内部类会带有一个对外部类的对象的引用,所以内部类可以访问外部类成员属性和成员函数
class Outer {
private val bar: Int = 1
var v = "成员属性"
/**嵌套内部类**/
inner class Inner {
fun foo() = bar // 访问外部类成员
fun innerTest() {
var o = this@Outer //获取外部类的成员变量
println("内部类可以引用外部类的成员,例如:" + o.v)
}
}
}
fun main(args: Array<String>) {
val demo = Outer().Inner().foo()
println(demo) // 1
val demo2 = Outer().Inner().innerTest()
println(demo2) // 内部类可以引用外部类的成员,例如:成员属性
}
为了消除歧义,要访问来自外部作用域的 this,我们使用this@label,其中 @label 是一个 代指 this 来源的标签。
- 匿名内部类
使用对象表达式来创建匿名内部类:
class Test {
var v = "成员属性"
fun setInterFace(test: TestInterFace) {
test.test()
}
}
/**
* 定义接口
*/
interface TestInterFace {
fun test()
}
fun main(args: Array<String>) {
var test = Test()
/**
* 采用对象表达式来创建接口对象,即匿名内部类的实例。
*/
test.setInterFace(object : TestInterFace {
override fun test() {
println("对象表达式创建匿名内部类的实例")
}
})
}
接口
使用 interface 关键字定义接口,允许方法有默认实现
interface MyInterface {
fun bar() // 未实现
fun foo() { //已实现
// 可选的方法体
println("foo")
}
}
实现接口
一个类或者对象可以实现一个或多个接口
class Child : MyInterface {
override fun bar() {
// 方法体
}
}
- 函数重写
实现多个接口时,可能会遇到同一方法继承多个实现的问题
interface A {
fun foo() { print("A") } // 已实现
fun bar() // 未实现,没有方法体,是抽象的
}
interface B {
fun foo() { print("B") } // 已实现
fun bar() { print("bar") } // 已实现
}
class C : A {
override fun bar() { print("bar") } // 重写
}
class D : A, B {
override fun foo() {
super<A>.foo()
super<B>.foo()
}
override fun bar() {
super<B>.bar()
}
}
fun main(args: Array<String>) {
val d = D()
d.foo();
d.bar();
}