7. @2017 PKLOT CO., LTD
HOW KOTLIN
▸ Install Android Studio Plugin “Kotlin”
▸ cmd + shift + a -> Configure kotlin in Project
▸ classpth : “org.jetbrains.kotlin:lotlin-gradle-plugin:1.0.7”
▸ apply plugin: ‘kotlin-android’
▸ compile ‘org.jetbrains.kotlin:kotlin-stdlib:1.0.7’
8. @2017 PKLOT CO., LTD
JAVA TO KOTLIN
▸ 遇到不知道如何表達的 kotlin 語法可以先透過轉換觀察
▸ cmd + shift + a -> Convert Java File to Kotlin
▸ ⼀一鍵完成
9. @2017 PKLOT CO., LTD
KOTLIN FEATURE
▸ Multiple class in one file
▸ Data Class
▸ Properties val/var
▸ Null Safety
▸ Smart Cast
▸ Method Extension / infix
▸ Lambda
▸ Operator Overloading
▸ Companion object
▸ Delegate
▸ Dependency Injection
10. @2017 PKLOT CO., LTD
MULTIPLE CLASS IN ONE FILE
▸ Class 的整理理更更有彈性
▸ 同質性⾼高且簡易易的 Class 可以放到⼀一起
▸ 提⾼高閱讀,不易易中斷思考
11. @2017 PKLOT CO., LTD
DATA CLASS
▸ hashCode()
▸ toString()
▸ equals()
▸ with properties
12. @2017 PKLOT CO., LTD
PROPERTIES
▸ var(variable)
▸ var age (compile error)
▸ var age:Int (compile error)
▸ var age = 1 (ok)
▸ age = 2 (ok)
▸ age = null (ok)
▸ var myAge = age (ok)
13. @2017 PKLOT CO., LTD
PROPERTIES
▸ val(value)
▸ val age (compile error)
▸ val age:Int (compile error)
▸ val age = 18 (ok)
▸ age = 19 (compile error)
14. @2017 PKLOT CO., LTD
NULL SAFETY
▸ NullPointerException
▸ ? -> nullable, default non null
▸ var order : Order? = Order()
▸ order?.price (null safety if order is null)
▸ order.price (compile error)
15. @2017 PKLOT CO., LTD
SMART CASE
▸ ClassCastException
▸ Stupid Case
if(exception instanceOf HttpException) {
HttpException httpException = (HttpException) exception
int code = httpException.getStatusCode();
}
16. @2017 PKLOT CO., LTD
SMART CASE
▸ Smart Case
when(exception) {
is HttpException -> {
int code = exception.statusCode
}
default -> {
// do something else
}
}
17. @2017 PKLOT CO., LTD
METHOD EXTENSION / INFIX
▸ Method Extension
var name : String = null
val isNull = name.isNullOrEmpty()
▸ In Java
if(name == null || name.length == 0)
StringUtils.isNullOrEmpty(name)
18. @2017 PKLOT CO., LTD
METHOD EXTENSION / INFIX
▸ How
fun CharSequence?.isNullOrEmpty() : Boolean
= this == null || this.lenght == 0
19. @2017 PKLOT CO., LTD
METHOD EXTENSION / INFIX
▸ Infix
▸ val isLike = “ABC” like “123-ABC”
infix fun String.like(it: String) : Boolean =
this.toUpperCase.contains(it)
▸ “name” to “Andy”
mapOf(
Pair(“nickName”, “andyang”),
“name” to “Andy”
)
25. @2017 PKLOT CO., LTD
LAMBDA
▸ Adapter OnItemClickListener in Kotlin
var onOrderItemClickListener : (order: Order) -> Unit
holder?.onClick{
onOrderItemClickListener.invoke(order)
}
holder?.onClick{
onOrderItemClickListener(order)
}
26. @2017 PKLOT CO., LTD
OPERATOR OVERLOADING
▸ a == b -> a.equals(b)
▸ a + b -> a.plus(b)
▸ a - b, a * b, a / b, a % b …..
▸ a..b -> a.rangeTo(b)
▸ a in b -> a.contains(b)
▸ a[i] -> a.get(i)
▸ a[i] = b -> a.set(i, b)
27. @2017 PKLOT CO., LTD
COMPANION OBJECT
class App : Application() {
companion object {
private var instance: Application? = null
fun instance() = instance
}
override fun onCreate() {
super.onCreate();
instance = this
}
}
28. @2017 PKLOT CO., LTD
PROPERTISE DELEGATE
class Delegate<T> : ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property : KPropety<*>) {
}
override fun setValue(thisRef: Any?, property : KPropety<*>, value: T) {
}
}
var name : String by Delegate()