Skip to content

Commit

Permalink
update Kotlin files
Browse files Browse the repository at this point in the history
  • Loading branch information
BetterZhang committed Jun 29, 2017
1 parent 43d0bb8 commit c701a74
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 89 deletions.
23 changes: 13 additions & 10 deletions src/com/betterzhang/learnkotlin/kotlin/Closure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : Kotlin闭包
*/

val plus = {x: Int, y: Int -> println("$x plus $y is ${x + y}")}
class Closure {
companion object {
val plus = {x: Int, y: Int -> println("$x plus $y is ${x + y}")}

val hello = {println("Hello Kotlin")}
val hello = {println("Hello Kotlin")}

fun main(args: Array<String>) {

{x: Int, y: Int ->
println("$x plus $y is ${x + y}")
}(2, 8) // 自执行的闭包

plus(2, 8)
hello()
@JvmStatic
fun main(args: Array<String>) {
{x: Int, y: Int ->
println("$x plus $y is ${x + y}")
}(2, 8) // 自执行的闭包

plus(2, 8)
hello()
}
}
}
17 changes: 10 additions & 7 deletions src/com/betterzhang/learnkotlin/kotlin/HighOrderFunc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : Kotlin高阶函数
*/

fun main(args: Array<String>) {

// 使用高阶函数
superFun("这是一个高阶函数", ::argFun)
superFun("这是一个高阶函数", { argFun()})
superFun("这是一个高阶函数") { argFun()}

class HighOrderFunc {
companion object {
@JvmStatic
fun main(args: Array<String>) {
// 使用高阶函数
superFun("这是一个高阶函数", ::argFun)
superFun("这是一个高阶函数", { argFun()})
superFun("这是一个高阶函数") { argFun()}
}
}
}

fun argFun() = " 我是高阶函数的参数"
Expand Down
11 changes: 8 additions & 3 deletions src/com/betterzhang/learnkotlin/kotlin/KotlinCallJava.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import com.betterzhang.learnkotlin.java.Person
* Desc : description
*/

fun main(args: Array<String>) {
var p = Person("Andrew Zhang", 25)
println(p.toString())
class KotlinCallJava {
companion object {
@JvmStatic
fun main(args: Array<String>) {
var p = Person("Andrew Zhang", 25)
println(p.toString())
}
}
}
31 changes: 17 additions & 14 deletions src/com/betterzhang/learnkotlin/kotlin/Lambda.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : description
*/

fun main(args: Array<String>) {
class Lambda {
companion object {
@JvmStatic
fun main(args: Array<String>) {
Thread(Runnable {
println("Hello Kotlin")
}).start()

Thread(Runnable {
println("Hello Kotlin")
}).start()
// Kotlin Lambda 表达式语法
val sum = {x: Int, y: Int -> x + y}
val sum1: (Int, Int) -> Int = {x, y -> x + y}
val sum2 = fun(x: Int, y: Int): Int {
return x + y
}

// Kotlin Lambda 表达式语法
val sum = {x: Int, y: Int -> x + y}
val sum1: (Int, Int) -> Int = {x, y -> x + y}
val sum2 = fun(x: Int, y: Int): Int {
return x + y
println(sum(2, 8))
println(sum1(3, 9))
println(sum2(5, 12))
}
}

println(sum(2, 8))
println(sum1(3, 9))
println(sum2(5, 12))

}
19 changes: 12 additions & 7 deletions src/com/betterzhang/learnkotlin/kotlin/Lazy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : Kotlin懒加载
*/

val lazyValue: String by lazy {
println("init") // 第一次使用时才被初始化
"Hello Kotlin"
}
class Lazy {
companion object {
val lazyValue: String by lazy {
println("init") // 第一次使用时才被初始化
"Hello Kotlin"
}

fun main(args: Array<String>) {
println(lazyValue)
println(lazyValue)
@JvmStatic
fun main(args: Array<String>) {
println(lazyValue)
println(lazyValue)
}
}
}
21 changes: 13 additions & 8 deletions src/com/betterzhang/learnkotlin/kotlin/MyClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ package com.betterzhang.learnkotlin.kotlin
*/
class MyClass {
fun foo() = println("member")
}

fun MyClass.bar() = println("extension")
companion object {

fun MyClass.foo() = println("extension foo")
fun MyClass.bar() = println("extension")

fun MyClass.foo(para: Int) = println("extension foo Int")
fun MyClass.foo() = println("extension foo")

fun MyClass.foo(para: Int) = println("extension foo Int")

@JvmStatic
fun main(args: Array<String>) {
MyClass().bar()
MyClass().foo() // 扩展函数与成员函数相同时,成员函数优先
MyClass().foo(10)
}
}

fun main(args: Array<String>) {
MyClass().bar()
MyClass().foo() // 扩展函数与成员函数相同时,成员函数优先
MyClass().foo(10)
}
15 changes: 11 additions & 4 deletions src/com/betterzhang/learnkotlin/kotlin/Nested.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : Kotlin嵌套函数
*/

fun main(args: Array<String>) {
fun sayHello() {
println("Hello Kotlin")
class Nested {

companion object {
@JvmStatic
fun main(args: Array<String>) {
fun sayHello() {
println("Hello Kotlin")
}
sayHello()
}
}
sayHello()

}
20 changes: 13 additions & 7 deletions src/com/betterzhang/learnkotlin/kotlin/Observable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import kotlin.properties.Delegates
* Time : 2017/06/28 下午 2:12
* Desc : Kotlin observable 观察者
*/
class Person2 {
class Observable {

public var name: String by Delegates.observable("init...") {
property, oldValue, newValue -> println("property: $property, oldValue: $oldValue, newValue: $newValue")
}
}
fun main(args: Array<String>) {
val person = Person2()
println(person.name)

person.name = "Andrew Zhang"
person.name = "Kotlin"
companion object {
@JvmStatic
fun main(args: Array<String>) {
val person = Observable()
println(person.name)

person.name = "Andrew Zhang"
person.name = "Kotlin"
}
}

}
11 changes: 7 additions & 4 deletions src/com/betterzhang/learnkotlin/kotlin/Outer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class Outer {
fun foo() = bar
}

}
companion object {
@JvmStatic
fun main(args: Array<String>) {
println(Outer.Nested().foo())
println(Outer().Inner().foo())
}
}

fun main(args: Array<String>) {
println(Outer.Nested().foo())
println(Outer().Inner().foo())
}
27 changes: 15 additions & 12 deletions src/com/betterzhang/learnkotlin/kotlin/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : 匿名内部类
*/

interface OnClickListener {
fun onClick()
}

class View {

interface OnClickListener {
fun onClick()
}

var listener: OnClickListener? = null

fun setOnClickListener(listener: OnClickListener) {
this.listener = listener
}

}

fun main(args: Array<String>) {
val view = View()
view.setOnClickListener(object: OnClickListener {
override fun onClick() {
TODO("not implemented")
companion object {
@JvmStatic
fun main(args: Array<String>) {
val view = View()
view.setOnClickListener(object: OnClickListener {
override fun onClick() {
TODO("not implemented")
}
})
}
})
}

}
23 changes: 14 additions & 9 deletions src/com/betterzhang/learnkotlin/kotlin/WrapFunc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ package com.betterzhang.learnkotlin.kotlin
* Desc : Kotlin泛型函数
*/

fun main(args: Array<String>) {
val list = singletonList<String>("Kotlin", "Java")
for (item in list) {
println(item)
}
}
class WrapFunc {

fun <T> singletonList(item1: T, item2: T): List<T> {
return arrayListOf(item1, item2)
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
val list = singletonList<String>("Kotlin", "Java")
for (item in list) {
println(item)
}
}

private fun <T> singletonList(item1: T, item2: T): List<T> {
return arrayListOf(item1, item2)
}
}
}
13 changes: 9 additions & 4 deletions src/com/betterzhang/learnkotlin/kotlin/Wrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ package com.betterzhang.learnkotlin.kotlin
* Time : 2017/06/28 下午 1:18
* Desc : 泛型
*/
class Wrapper<T>(val item: T)
class Wrapper<T>(val item: T) {

companion object {
@JvmStatic
fun main(args: Array<String>) {
var wrapper = Wrapper("Hello Kotlin")
println(wrapper.item)
}
}

fun main(args: Array<String>) {
var wrapper = Wrapper("Hello Kotlin")
println(wrapper.item)
}

0 comments on commit c701a74

Please sign in to comment.