Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Kotlin 初體驗

380 Aufrufe

Veröffentlicht am

Android development
kotlin

Veröffentlicht in: Ingenieurwesen
  • Als Erste(r) kommentieren

Kotlin 初體驗

  1. 1. KOTLIN初體驗 andyang@TADSG
  2. 2. ABOUT ME ▸ Andy ▸ @ 停⾞車車⼤大聲公 ▸ @ Android Developer 讀書會 (TADSG) ▸ @ Android Code Club (週三晚上)
  3. 3. 有⼈人聽過 KOTLIN 嗎?
  4. 4. 有⼈人⽤用過 KOTLIN 嗎?
  5. 5. ⽂文字 HELLO KOTLIN ▸ JetBrains 開發的 JVM 語⾔言 ▸ 包含很多 Effective Java 的思想在裡⾯面 ▸ Java 開發者的夢幻語⾔言 ?!
  6. 6. ⽂文字 WHY KOTLIN ▸ Short Code ▸ Null Safety ▸ Smart Cast ▸ Lambda (Functional Programing) ▸ Method Extension ▸ Hybrid with Java ▸ Default final ▸ Everything is Object
  7. 7. ⽂文字 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. 8. ⽂文字 JAVA TO KOTLIN ▸ 遇到不知道如何表達的 kotlin 語法可以先透過轉換觀察 ▸ cmd + shift + a -> Convert Java File to Kotlin ▸ ⼀一鍵完成
  9. 9. ⽂文字 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. 10. ⽂文字 MULTIPLE CLASS IN ONE FILE ▸ Class 的整理理更更有彈性 ▸ 同質性⾼高且簡易易的 Class 可以放到⼀一起 ▸ 提⾼高閱讀,不易易中斷思考
  11. 11. ⽂文字 DATA CLASS ▸ hashCode() ▸ toString() ▸ equals() ▸ with properties
  12. 12. ⽂文字 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. 13. ⽂文字 PROPERTIES ▸ val(value) ▸ val age (compile error) ▸ val age:Int (compile error) ▸ val age = 18 (ok) ▸ age = 19 (compile error)
  14. 14. ⽂文字 NULL SAFETY ▸ NullPointerException ▸ ? -> nullable, default non null ▸ var order : Order? = Order() ▸ order?.price (null safety if order is null) ▸ order.price (compile error)
  15. 15. ⽂文字 SMART CASE ▸ ClassCastException ▸ Stupid Case if(exception instanceOf HttpException) { HttpException httpException = (HttpException) exception int code = httpException.getStatusCode(); }
  16. 16. ⽂文字 SMART CASE ▸ Smart Case when(exception) { is HttpException -> { int code = exception.statusCode } default -> { // do something else } }
  17. 17. ⽂文字 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. 18. ⽂文字 METHOD EXTENSION / INFIX ▸ How fun CharSequence?.isNullOrEmpty() : Boolean = this == null || this.lenght == 0
  19. 19. ⽂文字 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” )
  20. 20. ⽂文字 LAMBDA ▸ 不必再等 Java 8 到天荒地老 ▸ 不⽤用 Retrolambda ▸ Kotlin 內建 lambda
  21. 21. ⽂文字 LAMBDA ▸ setOnClickListener() view.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Toast.makeToast(…..).show() } })
  22. 22. ⽂文字 LAMBDA ▸ setOnClickListener() view.setOnClickListener({ view -> Toast.makeToast(…..).show() }) view.setOnClickListener({ Toast.makeToast(…..).show() })
  23. 23. ⽂文字 LAMBDA ▸ setOnClickListener() view.setOnClickListener{ Toast.makeToast(…..).show()} view.onClick{ Toast.makeToast(…..).show()}
  24. 24. ⽂文字 LAMBDA ▸ Adapter OnItemClickListener interface OnOrderItemClickListener{ onOrderItemClick(Order order); } OnOrderItemClickListener onOrderItemClickListener; ‣ interface, setter, init, invoke
  25. 25. ⽂文字 LAMBDA ▸ Adapter OnItemClickListener in Kotlin var onOrderItemClickListener : (order: Order) -> Unit holder?.onClick{ onOrderItemClickListener.invoke(order) } holder?.onClick{ onOrderItemClickListener(order) }
  26. 26. ⽂文字 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. 27. ⽂文字 COMPANION OBJECT class App : Application() { companion object { private var instance: Application? = null fun instance() = instance } override fun onCreate() { super.onCreate(); instance = this } }
  28. 28. ⽂文字 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()
  29. 29. ⽂文字 PROPERTISE DELEGATE ‣ Demo Delegate SharedPreferences
  30. 30. ⽂文字 DEPENDENCY INJECTION ‣ In MPV Pattern ‣ Presenter need inject Model ‣ Repository(NetworkService networkService, LocalData localData) ‣ LocalData(Content context) ‣ Presenter(View view, Repository repository)
  31. 31. ⽂文字 DEPENDENCY INJECTION ‣ new Presenter ‣ In Java Presenter presenter = new Presenter(new Repository(new NetworkService(), new LocalData(context))); // so ugly Presenter presenter = PresenterFactory.create();
  32. 32. ⽂文字 DEPENDENCY INJECTION ‣ In kotlin ‣ Repository(networkService : NetworkService = NetworkService(), localData : LocalData = LocalData()) ‣ LocalData(context : Content = App.instance()) ‣ Presenter(view : View, repository : Repository = Repository())
  33. 33. ⽂文字 DEPENDENCY INJECTION ‣ In kotlin ‣ val presenter = Presenter(view) ‣ for testing ‣ val presenter = Presenter(view, mockRepository)
  34. 34. ⽂文字 REFERENCE ▸ Kotlin official guide ▸ https://kotlinlang.org/docs/reference/ ▸ Kotlin For Android Developer (e-book) ▸ https://antonioleiva.com/kotlin-android-developers- book/
  35. 35. 學習⼀一⾨門語⾔言最快的 ⽅方式就是只能⽤用它! andyang ⽂字
  36. 36. Q&A

×