├── .gitignore ├── 01_变量_空值安全 └── Hello.kt ├── 02_函数简介 └── Func.kt ├── 03_高阶函数_lambda表达式 ├── Closure.kt ├── HOF.jar └── HigherOrderFunction.kt ├── 04_类_继承_扩展 ├── A.kt ├── C.kt ├── Extension.kt ├── Interface.kt ├── META-INF │ └── main.kotlin_module └── Person.kt ├── 05_数据类_object_枚举 ├── .DS_Store ├── Companion.kt ├── Enum.kt ├── Object.kt ├── Outer.kt ├── Person.kt ├── Sealed.kt └── Singleton.kt ├── 06_集合_控制流 ├── Collection.kt ├── ControlFlow.kt └── META-INF │ └── main.kotlin_module ├── 07_操作符重载 └── OperatorOverload.kt ├── 08_泛型 ├── .idea │ ├── kotlinc.xml │ ├── libraries │ │ └── KotlinJavaRuntime.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── 08_Generic.iml └── src │ └── com │ └── youngfeng │ └── kotlin │ ├── Generic.kt │ └── Main.java ├── 09_代理 ├── .idea │ ├── kotlinc.xml │ ├── libraries │ │ └── KotlinJavaRuntime.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── 09_Delegate.iml └── src │ └── com │ └── youngfeng │ └── kotlin │ ├── Moveable.kt │ ├── Person.kt │ └── Tank.kt ├── 10_协程 ├── .gradle │ ├── 3.5-rc-2 │ │ ├── file-changes │ │ │ └── last-build.bin │ │ └── taskHistory │ │ │ ├── fileHashes.bin │ │ │ ├── fileSnapshots.bin │ │ │ ├── taskHistory.bin │ │ │ └── taskHistory.lock │ └── buildOutputCleanup │ │ ├── built.bin │ │ ├── cache.properties │ │ └── cache.properties.lock ├── .idea │ ├── .name │ ├── compiler.xml │ ├── gradle.xml │ ├── libraries │ │ ├── Gradle__junit_junit_4_12.xml │ │ ├── Gradle__org_hamcrest_hamcrest_core_1_3.xml │ │ ├── Gradle__org_jetbrains_annotations_13_0.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_2_40.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_2_40.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_2_40.xml │ │ └── Gradle__org_jetbrains_kotlinx_kotlinx_coroutines_core_0_22_5.xml │ ├── misc.xml │ ├── modules.xml │ ├── modules │ │ ├── coroutine.iml │ │ ├── coroutine_main.iml │ │ └── coroutine_test.iml │ └── workspace.xml ├── build.gradle ├── build │ └── kotlin-build │ │ └── version.txt ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── kotlin │ └── coroutine │ └── Coroutines.kt ├── 11_析构_注解_异常处理 ├── .idea │ ├── kotlinc.xml │ ├── libraries │ │ └── KotlinJavaRuntime.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── 11_De_An_CE.iml └── src │ └── com │ └── youngfeng │ └── kotlin │ ├── Annotation.kt │ ├── Destructure.kt │ ├── Exception.kt │ └── FileAnno.kt ├── 12_Java互通的那些事 ├── .idea │ ├── kotlinc.xml │ ├── libraries │ │ └── KotlinJavaRuntime.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── 12_Java_Interop.iml └── src │ └── com │ └── youngfeng │ └── kotlin │ ├── Client.java │ ├── F1.kt │ ├── F2.kt │ └── JavaInterop.kt ├── 13_控制台版本QQ聊天室实战 ├── .gradle │ ├── 3.5-rc-2 │ │ ├── file-changes │ │ │ └── last-build.bin │ │ └── taskHistory │ │ │ ├── fileHashes.bin │ │ │ ├── fileSnapshots.bin │ │ │ ├── taskHistory.bin │ │ │ └── taskHistory.lock │ └── buildOutputCleanup │ │ ├── built.bin │ │ ├── cache.properties │ │ └── cache.properties.lock ├── .idea │ ├── .name │ ├── compiler.xml │ ├── gradle.xml │ ├── libraries │ │ ├── Gradle__junit_junit_4_12.xml │ │ ├── Gradle__org_hamcrest_hamcrest_core_1_3.xml │ │ ├── Gradle__org_jetbrains_annotations_13_0.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_2_40.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_2_40.xml │ │ └── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_2_40.xml │ ├── misc.xml │ ├── modules.xml │ ├── modules │ │ ├── chatgroup.iml │ │ ├── chatgroup_main.iml │ │ └── chatgroup_test.iml │ └── workspace.xml ├── build.gradle ├── build │ └── kotlin-build │ │ └── version.txt ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── kotlin │ └── chatgroup │ ├── config │ └── Config.kt │ ├── main │ ├── Client.kt │ └── Server.kt │ ├── model │ ├── Command.kt │ └── Msg.kt │ └── util │ ├── IOUtils.kt │ └── TextUtil.kt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | out/ 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /01_变量_空值安全/Hello.kt: -------------------------------------------------------------------------------- 1 | var str: String? = null 2 | var str1: String = "Hello, world" 3 | var isRight: Boolean? = true 4 | var str2 = "abc" 5 | var str3 = null // 类型Nothing 6 | 7 | class A { 8 | lateinit var str4: String 9 | } 10 | 11 | fun main(args: Array) { 12 | println("Hello, world") 13 | println(str?.length) // if (str != null) str!!.length else null 14 | if (null != str) { 15 | println(str!!.length) 16 | } 17 | 18 | if (null != isRight && isRight!!) { 19 | println("isRight = true") 20 | } 21 | 22 | if (isRight ?: false) { 23 | println("isRight = true") 24 | } 25 | 26 | // str2 = 1 27 | println(str2) 28 | 29 | var s = "Scott Smith" 30 | println("Hello, $s") 31 | println("${opt()}gmail.com") 32 | 33 | val a = A() 34 | if (a::str4.isInitialized) { 35 | println("str4已经完成了初始化: ${a.str4}") 36 | } else { 37 | println("str4还没有完成初始化") 38 | } 39 | } 40 | 41 | fun opt(): String { 42 | return "@" 43 | } 44 | -------------------------------------------------------------------------------- /02_函数简介/Func.kt: -------------------------------------------------------------------------------- 1 | fun argsLength(vararg args: String) { 2 | println("args size = ${args.size}") 3 | } 4 | 5 | fun sum(x: Int, y: Int) = x + y 6 | 7 | fun canAdd(x: Int): Boolean { 8 | fun isOdd(x: Int): Boolean { 9 | return x % 2 != 0 10 | } 11 | 12 | return isOdd(x) 13 | } 14 | 15 | fun f(n: Int): Int { 16 | if (n == 1) return 1 17 | return n * f(n - 1) 18 | } 19 | 20 | fun main(args: Array) { 21 | val arr = arrayOf("a", "b", "c") 22 | argsLength(*arr) 23 | 24 | println(sum(x = 1, y = 1)) 25 | println(canAdd(10)) 26 | } 27 | -------------------------------------------------------------------------------- /03_高阶函数_lambda表达式/Closure.kt: -------------------------------------------------------------------------------- 1 | 2 | class A {} 3 | 4 | fun doSomething() { 5 | var a = A() 6 | val action = { index: Int -> 7 | // 这在Java语言中是不允许的 8 | a = A() 9 | } 10 | } -------------------------------------------------------------------------------- /03_高阶函数_lambda表达式/HOF.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuanhoujun/kotlin-video-tutorial/8da7fd14f8ee8eaf705b1a4d4a3b6f6fcf1468a9/03_高阶函数_lambda表达式/HOF.jar -------------------------------------------------------------------------------- /03_高阶函数_lambda表达式/HigherOrderFunction.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | fun predicate(t: T, condition: (t: T) -> Boolean): Boolean { 4 | return condition(t) 5 | } 6 | 7 | fun isEven(x: Int): Boolean { 8 | return x % 2 == 0 9 | } 10 | 11 | class A { 12 | 13 | fun isEven(x: Int): Boolean { 14 | return x % 2 == 0 15 | } 16 | } 17 | 18 | class B: (Int) -> Boolean { 19 | 20 | override fun invoke(x: Int): Boolean { 21 | return x % 2 == 0 22 | } 23 | } 24 | 25 | fun main(args: Array) { 26 | // 如果lambda表达式只有一个参数, 可以使用内置变量it指代这个唯一参数 27 | val isEven = predicate(10) { it % 2 == 0 } 28 | println(isEven) 29 | 30 | val sum: (Int, Int) -> Int = { x, y -> x + y } 31 | println("1 + 2 = ${sum(1, 2)}") 32 | 33 | println(predicate(10, ::isEven)) 34 | 35 | val a = A() 36 | println(predicate(10, a::isEven)) 37 | 38 | val f1 = A::isEven 39 | println(f1(a, 10)) 40 | 41 | val result = { x: Int, y: Int -> x + y } (1, 2) 42 | println("1 + 2 = $result") 43 | 44 | val f2 = fun(x: Int, y: Int): Int { 45 | return x + y 46 | } 47 | println(f2(1, 2)) 48 | println(f2.invoke(1, 2)) 49 | } -------------------------------------------------------------------------------- /04_类_继承_扩展/A.kt: -------------------------------------------------------------------------------- 1 | class A public constructor() { 2 | 3 | init { 4 | f() 5 | } 6 | 7 | val a = "a" 8 | 9 | fun f() { 10 | println(a) 11 | } 12 | } 13 | 14 | fun main(args: Array) { 15 | A() 16 | } -------------------------------------------------------------------------------- /04_类_继承_扩展/C.kt: -------------------------------------------------------------------------------- 1 | open class C { 2 | open var y: Int = 0 3 | 4 | constructor(x: Int) { 5 | println("The secondary constructor, $x") 6 | } 7 | 8 | init { 9 | println("This is a init block") 10 | } 11 | 12 | open fun foo() { 13 | println("foo in C") 14 | } 15 | } 16 | 17 | class C1(x: Int): C(x) { 18 | override var y: Int = super.y 19 | 20 | override fun foo() { 21 | println("foo in C1") 22 | } 23 | 24 | inner class CInner { 25 | 26 | fun f() { 27 | super@C1.foo() 28 | } 29 | } 30 | } 31 | 32 | fun main(args: Array) { 33 | val c: C = C1(10) 34 | 35 | c.foo() 36 | } -------------------------------------------------------------------------------- /04_类_继承_扩展/Extension.kt: -------------------------------------------------------------------------------- 1 | 2 | open class A { 3 | open var x: Int = 10 4 | 5 | 6 | } 7 | 8 | class A1: A() { 9 | 10 | } 11 | 12 | 13 | 14 | val A.y: Int 15 | get() = 100 16 | 17 | val A.x: Int 18 | get() = 110 19 | 20 | fun T.sayHello(name: String) { 21 | println("Hello, $name") 22 | } 23 | 24 | // DR: Dispatch Receiver, ER: Extension Receiver 25 | // ER:静态解析,接收者类型由声明时决定 26 | // DR:动态解析的,接收类型由运行时决定 27 | 28 | // A称之为printX的接收者(Receiver) 29 | fun A.printX() { 30 | println("x = ${this.x}") 31 | } 32 | 33 | fun A1.printX() { 34 | println("x = ${this.x} in A1") 35 | } 36 | 37 | fun f(a: A) { 38 | a.printX() 39 | } 40 | 41 | 42 | open class C {} 43 | class C1: C() { 44 | 45 | } 46 | 47 | open class D { 48 | 49 | open fun C.foo() { 50 | println("foo in C, declared in D") 51 | } 52 | 53 | open fun C1.foo() { 54 | println("foo in C1, declared in D") 55 | } 56 | 57 | fun call(c: C) { 58 | c.foo() 59 | } 60 | } 61 | 62 | class D1: D() { 63 | 64 | override fun C.foo() { 65 | println("foo in C, declared in D1") 66 | } 67 | 68 | override fun C1.foo() { 69 | println("foo in C1, declared in D1") 70 | } 71 | } 72 | 73 | 74 | fun main(arg: Array) { 75 | val a = A() 76 | a.printX() 77 | 78 | println(a.y) 79 | 80 | 81 | // DR: D, ER: C 82 | D().call(C()) 83 | 84 | // DR: D, ER: C1 85 | D().call(C1()) 86 | 87 | // DR: D1, ER: C 88 | D1().call(C()) 89 | } -------------------------------------------------------------------------------- /04_类_继承_扩展/Interface.kt: -------------------------------------------------------------------------------- 1 | 2 | interface Moveable { 3 | 4 | fun move() 5 | 6 | fun defaultImpl() { 7 | println("Moveable default implementation... ") 8 | } 9 | } 10 | 11 | class Tank: Moveable { 12 | 13 | override fun move() { 14 | println("Tank moving...") 15 | } 16 | } 17 | 18 | interface A { 19 | 20 | fun foo() { 21 | println("foo in A") 22 | } 23 | 24 | fun f() 25 | } 26 | 27 | interface B { 28 | 29 | fun foo() { 30 | println("foo in B") 31 | } 32 | 33 | fun f() 34 | } 35 | 36 | class C: A, B { 37 | 38 | override fun f() { 39 | super.foo() 40 | super.foo() 41 | } 42 | 43 | override fun foo() { 44 | 45 | } 46 | } 47 | 48 | 49 | fun main(args: Array) { 50 | val m: Moveable = Tank() 51 | 52 | m.move() 53 | m.defaultImpl() 54 | 55 | C().f() 56 | } -------------------------------------------------------------------------------- /04_类_继承_扩展/META-INF/main.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 |  3 |  InterfaceKt -------------------------------------------------------------------------------- /04_类_继承_扩展/Person.kt: -------------------------------------------------------------------------------- 1 | class Person public constructor(var age: Int, var name: String) { 2 | 3 | var gender: Int = -1 4 | private set 5 | get() { 6 | return field 7 | } 8 | 9 | init { 10 | println("The primary constructor...") 11 | } 12 | 13 | init { 14 | println("This is a init block too") 15 | } 16 | 17 | constructor(name: String): this(18, name) { 18 | println("The secondary constructor...") 19 | } 20 | } 21 | 22 | 23 | fun main(args: Array) { 24 | val a = Person(18, "Scott Smith") 25 | a.age = 28 // a.setAge(28) 26 | println(a.age) // a.getAge() 27 | 28 | a.gender = 1 29 | } 30 | 31 | -------------------------------------------------------------------------------- /05_数据类_object_枚举/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuanhoujun/kotlin-video-tutorial/8da7fd14f8ee8eaf705b1a4d4a3b6f6fcf1468a9/05_数据类_object_枚举/.DS_Store -------------------------------------------------------------------------------- /05_数据类_object_枚举/Companion.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Person { 4 | 5 | companion object { 6 | @JvmStatic 7 | fun create() = Person() 8 | } 9 | } -------------------------------------------------------------------------------- /05_数据类_object_枚举/Enum.kt: -------------------------------------------------------------------------------- 1 | 2 | enum class Direction { 3 | EAST, SOUTH, WEST, NORTH 4 | } 5 | 6 | 7 | enum class Operation(x: Int) { 8 | ADD(1) { 9 | override fun action() = 1 10 | }, 11 | MINUS(2) { 12 | override fun action() = 2 13 | }; 14 | 15 | abstract fun action(): Int 16 | } 17 | 18 | fun main(args: Array) { 19 | println(enumValues().joinToString { "${it.ordinal} : ${it.name}"}) 20 | } -------------------------------------------------------------------------------- /05_数据类_object_枚举/Object.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | fun main(args: Array) { 4 | val point = object { 5 | var x: Int = 0 6 | var y: Int = 0 7 | } 8 | 9 | println(point.x) 10 | } 11 | 12 | class Bar { 13 | 14 | fun foo() = object { 15 | var x: Int = 1 16 | } 17 | 18 | 19 | private fun foo1() = object { 20 | var x: Int = 1 21 | } 22 | 23 | fun baz() { 24 | // val x1 = foo().x 25 | val x2 = foo1().x 26 | } 27 | } -------------------------------------------------------------------------------- /05_数据类_object_枚举/Outer.kt: -------------------------------------------------------------------------------- 1 | class Outer { 2 | 3 | fun foo() { 4 | println("Outer foo()") 5 | } 6 | 7 | inner class Inner { 8 | 9 | fun f() { 10 | foo() 11 | } 12 | } 13 | } 14 | 15 | // 嵌套类 16 | // val inner = Outer.Inner() 17 | // 内部类 18 | val inner = Outer().Inner() 19 | 20 | // {id: xx, name: xx} => Person() => 数据类 -------------------------------------------------------------------------------- /05_数据类_object_枚举/Person.kt: -------------------------------------------------------------------------------- 1 | data class Person(var age: Int = 0, var name: String = "") { 2 | 3 | } 4 | 5 | 6 | fun main(args: Array) { 7 | val person = Person(28, "Scott Smith") 8 | val (_, name) = person 9 | println("$name") 10 | } -------------------------------------------------------------------------------- /05_数据类_object_枚举/Sealed.kt: -------------------------------------------------------------------------------- 1 | 2 | sealed class Operation(x: Int) { 3 | class Add(x: Int): Operation(x) 4 | class Minus(x: Int): Operation(x) 5 | class Times(x: Int): Operation(x) 6 | object OtherOperation: Operation(0) 7 | } -------------------------------------------------------------------------------- /05_数据类_object_枚举/Singleton.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Car {} 4 | 5 | object Factory { 6 | 7 | @JvmStatic 8 | fun create(): Car { 9 | return Car() 10 | } 11 | } 12 | 13 | 14 | val car = Factory.create() -------------------------------------------------------------------------------- /06_集合_控制流/Collection.kt: -------------------------------------------------------------------------------- 1 | 2 | fun main(args: Array) { 3 | val list = listOf("b", "a", "c") 4 | 5 | val mutableList = mutableListOf("b", "a", "c") 6 | mutableList.add("d") 7 | 8 | println(mutableList.filter { it != "a" }) 9 | 10 | println(mutableList.filterIndexed { index, e -> index > 0 }) 11 | 12 | println(mutableList.slice(0..2)) 13 | 14 | println(mutableList.take(2)) 15 | 16 | println(mutableList.sorted()) 17 | 18 | val m1 = mutableList.associateBy({ e -> "$e@"}) { it } 19 | println(m1) 20 | 21 | val list2 = mutableListOf("abc", "def") 22 | println(list2.flatMap { it.toList() }.map { "$it@" }) 23 | 24 | println(list2.map { "$it@"}) 25 | 26 | val list3 = mutableListOf(2, 1, 3, 4) 27 | 28 | println(list3.groupBy { if (it % 2 == 0) "偶数" else "奇数" }) 29 | 30 | val list4 = mutableListOf(3, 1, 5, 6) 31 | 32 | // 交集 33 | val s1 = list3 intersect list4 34 | println(s1) 35 | 36 | // 并集 37 | val s2 = list3 union list4 38 | println(s2) 39 | 40 | // 子集 41 | val s3 = list3 subtract list4 42 | println(s3) 43 | 44 | // 子集的补集 45 | val s4 = list4 subtract list3 46 | println(s4) 47 | 48 | // 交集的补集 49 | val s5 = s3 union s4 50 | println(s5) 51 | 52 | // 并集 53 | val s21 = list3 + list4 54 | println(s21) 55 | 56 | // 子集 57 | val s31 = list3 - list4 58 | println(s31) 59 | val s41 = list4 - list3 60 | println(s41) 61 | 62 | val s51 = s31 + s41 63 | println(s51) 64 | } -------------------------------------------------------------------------------- /06_集合_控制流/ControlFlow.kt: -------------------------------------------------------------------------------- 1 | 2 | 3 | fun main(args: Array) { 4 | var x = 6 5 | 6 | if (x % 2 == 0) { 7 | println("偶数") 8 | } else { 9 | println("奇数") 10 | } 11 | 12 | println(if (x % 2 == 0) "偶数" else "奇数") 13 | 14 | when (x) { 15 | 1 -> println("x = 1") 16 | in 2..5 -> println("x > 1 && x <= 5") 17 | in Int.MIN_VALUE..0 -> println("x < 1") 18 | in 6..Int.MAX_VALUE -> println("x > 5") 19 | } 20 | 21 | when { 22 | x == 1 -> println("x == 1") 23 | x > 1 && x <= 5 -> println("x > 1 && x <= 5") 24 | x < 1 -> println("x < 1") 25 | x > 5 -> println("x > 5") 26 | } 27 | 28 | var str = when (x) { 29 | 1 -> "1" 30 | in 2..Int.MAX_VALUE -> "x > 1" 31 | else -> "x < 1" 32 | } 33 | 34 | println(str) 35 | 36 | var list = listOf(2, 1, 3, 4) 37 | for (a in list) { 38 | println(a) 39 | } 40 | 41 | for (i in 9 downTo 1 step 2) { 42 | println("i = $i") 43 | } 44 | 45 | for ((index, value) in list.withIndex()) { 46 | println("$index : $value") 47 | } 48 | 49 | while (x > 6) { 50 | println("x > 6") 51 | } 52 | 53 | do { 54 | var y = op() 55 | println("y = $y") 56 | } while (y > 1) 57 | 58 | 59 | var arr1 = arrayOf(1, 2, 3, 4) 60 | var arr2 = arrayOf(5, 6, 7, 8) 61 | 62 | for (a in arr1) { 63 | loop@ for (b in arr2) { 64 | println("a = $a, b = $b") 65 | 66 | if (a >= 2) { 67 | break@loop 68 | } 69 | } 70 | } 71 | 72 | val s = sum(1, 2) s@ { x , y -> 73 | return@s x + y 74 | } 75 | println(s) 76 | 77 | // continue 78 | arr1.forEach loop@ { 79 | if (it == 2) return@loop 80 | println("it = $it") 81 | } 82 | 83 | // break 84 | run loop@ { 85 | arr1.forEach { 86 | if (it == 2) return@loop 87 | println("it = $it") 88 | } 89 | } 90 | 91 | println("Loop over") 92 | } 93 | 94 | fun sum(x: Int, y: Int, action: (x: Int, y: Int) -> Int): Int { 95 | return action(x, y) 96 | } 97 | 98 | var a = 100 99 | 100 | private fun op(): Int { 101 | a = a / 3 102 | return a 103 | } -------------------------------------------------------------------------------- /06_集合_控制流/META-INF/main.kotlin_module: -------------------------------------------------------------------------------- 1 |  2 |  3 |  CollectionKt -------------------------------------------------------------------------------- /07_操作符重载/OperatorOverload.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | class Number(var value: Int) { 4 | 5 | operator fun unaryMinus(): Number { 6 | this.value = -value 7 | return this 8 | } 9 | 10 | operator fun inc(): Number { 11 | return Number(this.value + 1) 12 | } 13 | 14 | operator fun plus(other: Number): Number { 15 | return Number(this.value + other.value) 16 | } 17 | 18 | operator fun rangeTo(other: Number): IntRange { 19 | return this.value..other.value 20 | } 21 | 22 | override fun equals(other: Any?): Boolean { 23 | if (other is Number) return this.value == other?.value 24 | return false 25 | } 26 | 27 | operator fun compareTo(other: Number): Int { 28 | return this.value - other.value 29 | } 30 | } 31 | 32 | 33 | 34 | class MyList { 35 | var mList = ArrayList() 36 | 37 | fun add(value: Int) { 38 | mList.add(value) 39 | } 40 | 41 | operator fun get(index: Int): Int { 42 | return mList.get(index) 43 | } 44 | 45 | operator fun invoke() { 46 | println("$this invoked") 47 | } 48 | } 49 | 50 | 51 | 52 | fun main(args: Array) { 53 | var number = Number(10) 54 | val oldNumber = number ++ 55 | println("Old value = ${oldNumber.value}, new value = ${number.value}") 56 | 57 | val number1 = Number(21) 58 | println((number + number1).value) 59 | 60 | println(number..number1) 61 | 62 | for (x in number..number1) { 63 | println("x = $x") 64 | } 65 | 66 | val list = MyList() 67 | list.add(2) 68 | list.add(3) 69 | println(list[0]) 70 | 71 | list() 72 | 73 | number.value = number1.value 74 | println(number == number1) 75 | 76 | 77 | number1.value = 21 78 | 79 | println(number > number1) 80 | } 81 | 82 | -------------------------------------------------------------------------------- /08_泛型/.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /08_泛型/.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /08_泛型/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /08_泛型/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /08_泛型/08_Generic.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /08_泛型/src/com/youngfeng/kotlin/Generic.kt: -------------------------------------------------------------------------------- 1 | package com.youngfeng.kotlin 2 | 3 | /** 4 | * 请描述使用该类使用方法!!! 5 | * 6 | * @author Scott Smith 2018-05-26 20:29 7 | */ 8 | 9 | // 声明区变量 10 | interface Collection { 11 | 12 | fun add(item: T) 13 | 14 | // fun get(index: Int): T 15 | } 16 | 17 | // out: 协变 => 生产者 => ? extends T 18 | // in:逆变 => 消费者 19 | // out: 返回值位置,in:参数位置 20 | 21 | interface Array2 { 22 | fun set(index: Int, item: T) 23 | fun get(index: Int): T 24 | } 25 | 26 | // 使用区变量 27 | fun copy(from: Array2, to: Array2) { 28 | } 29 | 30 | fun fill(arr: Array2, value: String) { 31 | 32 | } 33 | 34 | open class A {} 35 | open class B: A() 36 | 37 | fun foo(data: T) {} 38 | 39 | interface C {} 40 | open class D {} 41 | 42 | // T:实现接口C,并且继承类D 43 | fun bar(data: T) 44 | where T: C, 45 | T: D { 46 | 47 | } 48 | 49 | open class TUpper { 50 | fun f() {} 51 | } 52 | 53 | open class TUpper2: TUpper() 54 | 55 | class Foo { 56 | 57 | fun get(): T { 58 | } 59 | 60 | fun set(data: T) {} 61 | } 62 | 63 | fun main(args: kotlin.Array) { 64 | // val from: Array2? = null 65 | // val to: Array2? = null 66 | // 67 | // copy(from!!, to!!) 68 | // 69 | // val arr: Array2? = null 70 | // fill(arr!!, "") 71 | // 72 | // foo(B()) 73 | 74 | val foo: Foo<*> = Foo() 75 | val t = foo.get() 76 | t.f() 77 | foo.set(TUpper()) 78 | // val t = foo.get() 79 | // t.f() 80 | } -------------------------------------------------------------------------------- /08_泛型/src/com/youngfeng/kotlin/Main.java: -------------------------------------------------------------------------------- 1 | package com.youngfeng.kotlin; 2 | 3 | /** 4 | * 请描述使用该类使用方法!!! 5 | * 6 | * @author Scott Smith 2018-05-26 20:09 7 | */ 8 | public class Main { 9 | 10 | public static void main(String[] args) { 11 | Object[] arr = new String[3]; 12 | arr[0] = 1; 13 | System.out.println(arr[0]); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /09_代理/.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /09_代理/.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /09_代理/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /09_代理/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /09_代理/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 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 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 130 | 131 | 132 | 135 | 136 | 137 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 121 | 122 | 123 | 124 | 129 | 130 | 131 | 143 | 144 | 145 | 146 | 160 | 161 | 162 | 163 | 164 | 165 | 169 | 170 | 176 | 177 | 178 | 179 | 197 | 203 | 204 | 205 | 215 | 216 | 217 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 1527562765167 248 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 1.8 388 | 389 | 394 | 395 | 396 | 397 | 398 | 399 | -------------------------------------------------------------------------------- /11_析构_注解_异常处理/11_De_An_CE.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /11_析构_注解_异常处理/src/com/youngfeng/kotlin/Annotation.kt: -------------------------------------------------------------------------------- 1 | package com.youngfeng.kotlin 2 | 3 | import kotlin.reflect.KProperty 4 | 5 | /** 6 | * 请描述使用该类使用方法!!! 7 | * 8 | * @author Scott Smith 2018-05-29 11:12 9 | */ 10 | 11 | @Target(AnnotationTarget.ANNOTATION_CLASS, 12 | AnnotationTarget.FUNCTION, 13 | AnnotationTarget.CONSTRUCTOR, 14 | AnnotationTarget.PROPERTY, 15 | AnnotationTarget.PROPERTY_SETTER, 16 | AnnotationTarget.PROPERTY_GETTER, 17 | AnnotationTarget.VALUE_PARAMETER, 18 | AnnotationTarget.FIELD) 19 | @Retention(AnnotationRetention.RUNTIME) 20 | @MustBeDocumented 21 | annotation class Inject(val value: Int) 22 | 23 | // name: 1)构造函数参数中,2)成员变量中,3)setter/getter方法中 24 | class Bird(@property:Inject(10) var name: String) { 25 | @delegate:Inject(10) val age: Int by Delegate { 26 | 10 27 | } 28 | } 29 | 30 | private object UNINITIALIZE_VALUE 31 | 32 | class Delegate(init: ()->T) { 33 | private val initialize: ()->T = init 34 | private var _value: Any? = UNINITIALIZE_VALUE 35 | 36 | operator fun getValue(bird: Bird, property: KProperty<*>): T { 37 | if (_value == UNINITIALIZE_VALUE) { 38 | _value = initialize() 39 | } 40 | return _value as T 41 | } 42 | } 43 | 44 | fun @receiver:com.youngfeng.kotlin.Inject(10) Bird.fly() { 45 | println("I can fly") 46 | } 47 | 48 | fun main(args: Array) { 49 | val property: KProperty<*> = Bird::name 50 | println(property.annotations) 51 | } -------------------------------------------------------------------------------- /11_析构_注解_异常处理/src/com/youngfeng/kotlin/Destructure.kt: -------------------------------------------------------------------------------- 1 | package com.youngfeng.kotlin 2 | 3 | /** 4 | * 请描述使用该类使用方法!!! 5 | * 6 | * @author Scott Smith 2018-05-29 11:00 7 | */ 8 | data class Person(var name: String, var age: Int) { 9 | } 10 | 11 | fun main(args: Array) { 12 | val person = Person("Scott Smith", 30) 13 | val (name, age) = person 14 | println("name = $name, age = $age") 15 | 16 | val person2 = Person("Lily", 25) 17 | 18 | val list = listOf(person, person2) 19 | 20 | for ((name, _) in list) { 21 | println("name = $name") 22 | } 23 | 24 | val map = mapOf("k1" to "v1", "k2" to "v2") 25 | for ((key, value) in map) { 26 | println("key = $key, value = $value") 27 | } 28 | 29 | map.mapValues { (key, value) -> 30 | println("$key : $value") 31 | } 32 | } -------------------------------------------------------------------------------- /11_析构_注解_异常处理/src/com/youngfeng/kotlin/Exception.kt: -------------------------------------------------------------------------------- 1 | package com.youngfeng.kotlin 2 | 3 | /** 4 | * 请描述使用该类使用方法!!! 5 | * 6 | * @author Scott Smith 2018-05-29 11:46 7 | */ 8 | 9 | 10 | fun bar(value: Int) { 11 | if (value < 0) throw IllegalArgumentException() 12 | if (value % 2 != 0) throw RuntimeException() 13 | } 14 | 15 | 16 | fun baz(input: String): Int? { 17 | return try { input.toInt() } catch (e: NumberFormatException) { null } 18 | } 19 | 20 | fun bar1() { 21 | bar(11) 22 | } -------------------------------------------------------------------------------- /11_析构_注解_异常处理/src/com/youngfeng/kotlin/FileAnno.kt: -------------------------------------------------------------------------------- 1 | @file:JvmName("FileTest") 2 | 3 | package com.youngfeng.kotlin 4 | 5 | /** 6 | * 请描述使用该类使用方法!!! 7 | * 8 | * @author Scott Smith 2018-05-29 11:21 9 | */ 10 | 11 | fun foo() { 12 | println("Foo...") 13 | } -------------------------------------------------------------------------------- /12_Java互通的那些事/.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /12_Java互通的那些事/.idea/libraries/KotlinJavaRuntime.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /12_Java互通的那些事/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /12_Java互通的那些事/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /12_Java互通的那些事/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 23 | 24 | 25 | 28 | 29 | 30 | 40 | 41 | 42 | 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 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 |