包的定义与导入
包的声明应处于源文件顶部:
package com.test.learnkotlin
import com.test.learnkotlin.bean.Person
目录与包的结构无需匹配:源代码可以在文件系统的任意位置。
参考包–英文版
参考包–中文版
程序入口点
Kotlin 应用程序的入口点是 main 函数。
fun main(){
println("hello world !")
}
另一种形式的 main 接受可变数量的 String 参数。
fun main(args: Array<String>){
println(args.contentToString())
val person = Person("test")
println(person.toString())
}
函数
带有两个 Int 参数、返回 Int 的函数:
fun sum(a: Int, b: Int): Int {
return a + b
}
将表达式作为函数体、返回值类型自动推断的函数:
fun sum2(a: Int,b: Int) = a + b
函数返回无意义的值:
fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}")
}
Unit 返回类型可以省略:
fun printSum2(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}")
}
变量
定义只读局部变量使用关键字 val 定义。只能为其赋值一次。
val a: Int = 1 // 立即赋值
val b = 2 // 自动推断出 `Int` 类型
//val c1: Int // 提示错误信息:必须初始化
val c2: Int = 0 // 初始化
//c2 = 3 // 提示错误信息:Might be const
可重新赋值的变量使用 var 关键字:
fun main(args: Array<String>) {
var x = 5 // 自动推断出 `Int` 类型
x += 1
}
顶层变量
val PI = 3.14
var x = 0
fun incrementX() {
x += 1
}
创建类和实例
使用 class 关键字定义一个类
class Shape
类的属性可以在构造函数声明或主体中列出。
class Rectangle(var height: Double, var length: Double) {
var perimeter = (height + length) * 2
}
默认带参数的构造函数,在类里面可以直接使用
val rectangle = Rectangle(5.0, 2.0)
println("The perimeter is ${rectangle.perimeter}")
类之间的继承由冒号 : 声明。 默认情况下,类是最终的; 要使类可继承,请将其标记为 open
open class Shape{
}
class Rectangle(var height: Double, var length: Double):Shape() {
var perimeter = (height + length) * 2
}
注释
与大多数现代语言一样,Kotlin 支持单行(或行末)与多行(块)注释。
// 这是一个行注释
/* 这是一个多行的
块注释。 */
Kotlin 中的块注释可以嵌套。
/* 注释从这里开始
/* 包含嵌套的注释 */
并且在这里结束。 */
字符串模板
var a1 = 1
// simple name in template:
val s1 = "a1 is $a1"
println("s1:$s1")
a1 = 2
// arbitrary expression in template:
val s2 = "${s1.replace("is", "was")}, but now is $a1"
println("s2:$s2")
//s1:a1 is 1
//s2:a1 was 1, but now is 2
条件表达式
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
在 Kotlin 中,if 也可以用作表达式:
fun maxOf(a: Int, b: Int) = if (a > b) a else b
循环
// for 循环
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
}
//apple
//banana
//kiwifruit
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
//item at 0 is apple
//item at 1 is banana
//item at 2 is kiwifruit
// while 循环
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
//item at 0 is apple
//item at 1 is banana
//item at 2 is kiwifruit
when表达式
fun main(args: Array<String>) {
val describeText = describe("Hello")
println("describeText = $describeText")
//describeText = Greeting
}
fun describe(obj: Any): String =
when (obj) {
1 -> "One"
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
else -> "Unknown"
}
Ranges 使用区间
val x1 = 10
val y1 = 9
if (x1 in 1..y1+1) {
println("fits in range")
}
//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)
}
println()
for (x in 1..10 step 2) {
print(x)
}
println()
for (x in 9 downTo 0 step 3) {
print(x)
}
//-1 is out of range
//list size is out of valid list indices range, too
//12345
//13579
//9630
Collections 集合
val items = listOf("apple", "banana", "kiwifruit")
//遍历集合
for (item in items) {
println(item)
}
//apple
//banana
//kiwifruit
//使用 in 运算符检查集合是否包含对象。
when {
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
}
//apple is fine too
使用 lambda 表达式过滤和映射集合:
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
fruits
.filter { it.startsWith("a") }// 筛选 以 a 字母开头的元素
.sortedBy { it }//升序排序
.map { it.uppercase() }//转大写
.forEach { println(it) }
//APPLE
//AVOCADO
空值与 null 检测
当某个变量的值可以为 null 的时候,必须在声明处的类型后添加 ? 来标识该引用可为空。
如果 str 的内容不是数字返回 null:
fun parseInt(str: String): Int? {
return if (!str.isNotEmpty() && str.equals("1")) 1 else 0
}
//使用返回可空值的函数:
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 运算符检测一个表达式是否某类型的一个实例。 如果一个不可变的局部变量或属性已经判断出为某类型,那么检测后的分支中可以直接当作该类型使用,无需显式转换:
fun getStringLength1(obj: Any): Int? {
if (obj is String) {
// `obj` 在该条件分支内自动转换成 `String`
return obj.length
}
// 在离开类型检测分支后,`obj` 仍然是 `Any` 类型
return null
}
//或者
fun getStringLength2(obj: Any): Int? {
if (obj !is String) return null
// `obj` 在这一分支自动转换为 `String`
return obj.length
}
//甚至
fun getStringLength3(obj: Any): Int? {
// `obj` 在 `&&` 右边自动转换成 `String` 类型
if (obj is String && obj.length > 0) {
return obj.length
}
return null
}
源码-参考 01base 包里的 BasicSyntax01.kt文件
欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。