Kotlin(科特林)是一个用于现代多平台应用的静态编程语言,由 JetBrains 开发。
Kotlin可以编译成Java字节码,也可以编译成JavaScript,方便在没有JVM的设备上运行。除此之外Kotlin还可以编译成二进制代码直接运行在机器上(例如嵌入式设备或 iOS)。
Kotlin已正式成为Android官方支持开发语言。
基础语法
变量与常量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| fun main() {
var name: String = "Derry" name = "张三" name = "李四"
var info1 = "AAAA" var info2 = 'A' var info3:Int = 99
val age: Int = 99
var info4 = "LISI" }
class Test {
var info1 : String = "A"
val info2 : String = "B" }
|
函数方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
fun main(): Unit {
println(add(1, 1))
lenMethod(1, 2, 3, 4, 5, 6, 7)
val addMethod : (Int, Int) -> Int = {number1, number2 -> number1 + number2} val r= addMethod(9, 9) println(r) }
fun add(number1: Int, number2: Int): Int { return number1 + number2 }
fun add2(number1: Int, number2: Int) = number1 + number2
fun add3(number1: Int, number2: Int) = "AAA"
fun lenMethod(vararg value: Int) { for (i in value) { println(i) } }
|
字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| fun main() {
val name = "张三"
val age = 28
val sex = 'M'
val info = "ABCDEFG"
println("name:$name, age:$age, sex:$sex info:$info")
val infoMesage = """ AAAAAAAAAAA BBBBBBBBBBB CCCCCCCCCCC DDDDDDDDDDD EEEEEEEEEEE """ println(infoMesage)
val infoMesage2 = """ AAAAAAAAAAA BBBBBBBBBBB CCCCCCCCCCC DDDDDDDDDDD EEEEEEEEEEE """.trimIndent() println(infoMesage2)
val infoMesage3 = """ ?AAAAAAAAAAA ?BBBBBBBBBBB ?CCCCCCCCCCC ?DDDDDDDDDDD ?EEEEEEEEEEE """.trimMargin("?") println(infoMesage3)
val price = """ ${'$'}99999.99 """.trimIndent() println(price) }
|
NULL检查机制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| fun main() {
var info: String? = null
println(info?.length ?: "你很牛逼") }
fun testMethod(name: String) : Int? { if (name == "zs") { return 99999 } return null }
|
区间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| fun main() {
for (i in 1..9) { }
for (i in 9..1) { }
for (i in 9 downTo 1) { }
val value = 88 if (value in 1..100) { }
for (i in 1..20 step 2) { }
for (i in 1 until 10) { println(i) } }
|
比较与数组
比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| fun main() {
val name1: String = "张三" val name2: String = "张三"
println(name1.equals(name2)) println(name1 == name2)
val test1:Int? = 10000 val test2:Int? = 10000 println(test1 === test2) }
|
数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| fun main() { val numbers = arrayOf(1, 2, 3, 4, 5, 6, 7, 8) for (number in numbers) { }
val numbers2 = Array(10, {value: Int -> (value + 200) }) for (value in numbers2) { println(value) }
}
|
条件控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| fun main() { val number1: Int = 9999999 val number2: Int = 8888888
val maxValue = if (number1 > number2) number1 else number2
val max: Int = if (number1 > number2) { println("number1是最大的哦") number1 } else { println("number2是最大的哦") number2 }
val x = 80 val y = 29 if (x in 1..10 && y in 1..50) { } else { }
println()
val number = 3 val result = when (number) { 1 -> { println("很开心") "今天是星期一" 99 } 2 -> { println("很开心") "今天是星期二" 88 } 3 -> { println("很开心") "今天是星期三" true 100 } else -> 99 }
when (8) { 1, 2, 3, 4, 5, 6, 7 -> println("满足") else -> println("不满足") } }
|
循环与标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| fun main() {
tttt@ for (i in 1..20) {
for (j in 1..20) { println("i:$i, j:$j")
if (i == 5) {
break@tttt } }
}
var items = listOf<String>("李四", "张三", "王五") for (item in items) { println(item) }
items.forEach { println(it) }
for (index in items.indices) { println("下标:$index, 对应的值:${items[index]}") }
}
class Derry {
val I = "AAAA"
fun show() { println(I) println(this.I) println(this@Derry.I) }
}
|
类与对象
构造和继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| open class Person(id: Int) {
constructor(id: Int, name: String) : this(id) {
}
constructor(id: Int, sex: Char) : this(id) {
}
constructor() : this(787) {
}
}
|
1 2 3 4 5 6 7 8 9 10 11
| class Student(id: Int) : Person(id) {
lateinit var name : String var age: Int = 0 }
|
1 2 3 4 5 6 7 8 9 10 11
| fun main() {
val person = Person()
val person2 = Person(6465)
Person(464, "Derry") Person(464, 'M') }
|
接口与抽象
1 2 3 4 5 6 7 8 9 10 11 12
| interface Callback {
fun callbackMethod() : Boolean
}
interface Callback2 {
fun callbackMethod2() : Boolean
}
|
1 2 3 4 5 6 7 8
| abstract class Person : Callback , Callback2 {
abstract fun getLayoutID() : Int
abstract fun initView()
}
|
1 2 3 4 5 6 7 8
| class Student : Person() {
override fun getLayoutID(): Int = 888
override fun initView() { }
override fun callbackMethod(): Boolean = false }
|
数据类和单例
1 2 3 4 5 6 7 8
|
data class User(val id: Int, val name: String, val sex: Char)
|
1 2 3 4 5 6 7 8 9 10 11
| object MyEngine {
fun m() { println("M run") }
fun show() { println("我就只有一个实例") } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class SingletonDemo private constructor() { companion object { private var instance: SingletonDemo? = null get() { if (field == null) { field = SingletonDemo() } return field } @Synchronized fun get(): SingletonDemo{ return instance!! } }
}
|
1 2 3 4 5 6 7 8 9
| class SingletonDemo private constructor() { companion object { val instance: SingletonDemo by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { SingletonDemo() } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| fun main() {
val user = User(99, "lisi", 'M')
val(myID, myName, mySex) = user.copy() println("myID:$myID, myName:$myName, mySex:$mySex")
val(_, myName2, _) = user.copy() println("myName2:$myName2")
MyEngine.m() }
|
kotlin手动写单例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class NetManager {
object Holder {
val instance = NetManager()
}
companion object {
fun getInstance() : NetManager = Holder.instance }
fun show(name: String) { println("show:$name"); }
}
fun main() { val net = NetManager.getInstance() net.show("kt Derry1") }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class NetManager2 {
companion object {
private var instance: NetManager2? = null
fun getInstance(): NetManager2? { if (instance == null) { instance = NetManager2() }
return instance
}
}
fun show(name: String) { println("show:$name"); }
}
fun main() { val netManager = NetManager2.getInstance()
netManager?.show("AAA") }
|
嵌套类、内部类、嵌套函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class Test {
val I = "AAAA"
class Sub {
fun show() { println() }
class A {
class B {
class C {
}
}
}
}
inner class Sub2 {
fun show() { println(I) }
}
}
fun main() { fun a() {
fun b() {
fun c() {
fun d() {
}
}
}
} }
|