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

More Related Content

What's hot

Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
KAI CHU CHUNG
 
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Rajmahendra Hegde
 
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
e-Legion
 
Using Monitoring & Configuration Management to restart services.
Using Monitoring & Configuration Management to restart services.Using Monitoring & Configuration Management to restart services.
Using Monitoring & Configuration Management to restart services.
gregretkowski
 
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
ZeroTurnaround
 
Gradle - time for a new build
Gradle - time for a new buildGradle - time for a new build
Gradle - time for a new buildIgor Khotin
 
Build microservice with gRPC in golang
Build microservice with gRPC in golangBuild microservice with gRPC in golang
Build microservice with gRPC in golang
Ting-Li Chou
 
Using the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentUsing the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM Development
Schalk Cronjé
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
Bo-Yi Wu
 
Gdg cloud taipei ddt meetup #53 buildpack
Gdg cloud taipei ddt meetup #53 buildpackGdg cloud taipei ddt meetup #53 buildpack
Gdg cloud taipei ddt meetup #53 buildpack
KAI CHU CHUNG
 
Reactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event ProcessingReactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event Processing
Yoshifumi Kawai
 

What's hot (11)

Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
 
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
 
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
#MBLTdev: Разработка первоклассных SDK для Android (Twitter)
 
Using Monitoring & Configuration Management to restart services.
Using Monitoring & Configuration Management to restart services.Using Monitoring & Configuration Management to restart services.
Using Monitoring & Configuration Management to restart services.
 
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
 
Gradle - time for a new build
Gradle - time for a new buildGradle - time for a new build
Gradle - time for a new build
 
Build microservice with gRPC in golang
Build microservice with gRPC in golangBuild microservice with gRPC in golang
Build microservice with gRPC in golang
 
Using the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentUsing the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM Development
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
Gdg cloud taipei ddt meetup #53 buildpack
Gdg cloud taipei ddt meetup #53 buildpackGdg cloud taipei ddt meetup #53 buildpack
Gdg cloud taipei ddt meetup #53 buildpack
 
Reactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event ProcessingReactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event Processing
 

Similar to Kotlin初體驗

Kotlin 初體驗
Kotlin 初體驗Kotlin 初體驗
Kotlin 初體驗
哲偉 楊
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcampSummer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig
 
黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice
Fong Liou
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
André Pitombeira
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutes
Kai Koenig
 
Creative Solutions to Already Solved Problems II
Creative Solutions to Already Solved Problems IICreative Solutions to Already Solved Problems II
Creative Solutions to Already Solved Problems II
Gene Gotimer
 
Is writing performant code too expensive?
Is writing performant code too expensive? Is writing performant code too expensive?
Is writing performant code too expensive?
Tomasz Kowalczewski
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android development
GDGKuwaitGoogleDevel
 
2017: Kotlin - now more than ever
2017: Kotlin - now more than ever2017: Kotlin - now more than ever
2017: Kotlin - now more than ever
Kai Koenig
 
Grails EE
Grails EEGrails EE
Grails EE
GR8Conf
 
GR8Conf 2011: GContracts
GR8Conf 2011: GContractsGR8Conf 2011: GContracts
GR8Conf 2011: GContractsGR8Conf
 
Effective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiDEffective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiD
CODEiD PHP Community
 
Is writing performant code too expensive?
Is writing performant code too expensive? Is writing performant code too expensive?
Is writing performant code too expensive?
Tomasz Kowalczewski
 
Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with Kotlin
Kai Koenig
 
Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?
Kai Koenig
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
Srikanth Shreenivas
 
The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)
Michiel Rook
 
Clean Code
Clean CodeClean Code
Clean Code
Nascenia IT
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with Kotlin
Kai Koenig
 

Similar to Kotlin初體驗 (20)

Kotlin 初體驗
Kotlin 初體驗Kotlin 初體驗
Kotlin 初體驗
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcampSummer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcamp
 
黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice黑豹 ch4 ddd pattern pracrice
黑豹 ch4 ddd pattern pracrice
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutes
 
Smoke testing with Go
Smoke testing with GoSmoke testing with Go
Smoke testing with Go
 
Creative Solutions to Already Solved Problems II
Creative Solutions to Already Solved Problems IICreative Solutions to Already Solved Problems II
Creative Solutions to Already Solved Problems II
 
Is writing performant code too expensive?
Is writing performant code too expensive? Is writing performant code too expensive?
Is writing performant code too expensive?
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android development
 
2017: Kotlin - now more than ever
2017: Kotlin - now more than ever2017: Kotlin - now more than ever
2017: Kotlin - now more than ever
 
Grails EE
Grails EEGrails EE
Grails EE
 
GR8Conf 2011: GContracts
GR8Conf 2011: GContractsGR8Conf 2011: GContracts
GR8Conf 2011: GContracts
 
Effective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiDEffective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiD
 
Is writing performant code too expensive?
Is writing performant code too expensive? Is writing performant code too expensive?
Is writing performant code too expensive?
 
Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with Kotlin
 
Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
 
The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)
 
Clean Code
Clean CodeClean Code
Clean Code
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with Kotlin
 

More from 哲偉 楊

Specification unit test by Spek
Specification unit test by SpekSpecification unit test by Spek
Specification unit test by Spek
哲偉 楊
 
Code kata 的自我修煉
Code kata 的自我修煉Code kata 的自我修煉
Code kata 的自我修煉
哲偉 楊
 
Coding dojo
Coding dojoCoding dojo
Coding dojo
哲偉 楊
 
輕輕鬆鬆產生 changelog
輕輕鬆鬆產生 changelog輕輕鬆鬆產生 changelog
輕輕鬆鬆產生 changelog
哲偉 楊
 
Speed up add custom marker on google map
Speed up add custom marker on google mapSpeed up add custom marker on google map
Speed up add custom marker on google map
哲偉 楊
 
Spek
SpekSpek
Jenkins for android developer at TWJUG
Jenkins for android developer at TWJUGJenkins for android developer at TWJUG
Jenkins for android developer at TWJUG
哲偉 楊
 
自己的 Jenkins 自己來 for Android developer
自己的 Jenkins 自己來  for Android developer自己的 Jenkins 自己來  for Android developer
自己的 Jenkins 自己來 for Android developer
哲偉 楊
 
從開發到上線的華麗大冒險
從開發到上線的華麗大冒險從開發到上線的華麗大冒險
從開發到上線的華麗大冒險
哲偉 楊
 
Unit test and ui testing with cucumber
Unit test and ui testing with cucumberUnit test and ui testing with cucumber
Unit test and ui testing with cucumber
哲偉 楊
 
RxJava With retrolambda
RxJava With retrolambdaRxJava With retrolambda
RxJava With retrolambda
哲偉 楊
 
ORMLite Android
ORMLite AndroidORMLite Android
ORMLite Android
哲偉 楊
 
設計師合作經驗分享
設計師合作經驗分享設計師合作經驗分享
設計師合作經驗分享
哲偉 楊
 
Dog point
Dog pointDog point
Dog point
哲偉 楊
 
Gson
GsonGson
Hybrid design with bootstrap
Hybrid design with bootstrapHybrid design with bootstrap
Hybrid design with bootstrap
哲偉 楊
 

More from 哲偉 楊 (16)

Specification unit test by Spek
Specification unit test by SpekSpecification unit test by Spek
Specification unit test by Spek
 
Code kata 的自我修煉
Code kata 的自我修煉Code kata 的自我修煉
Code kata 的自我修煉
 
Coding dojo
Coding dojoCoding dojo
Coding dojo
 
輕輕鬆鬆產生 changelog
輕輕鬆鬆產生 changelog輕輕鬆鬆產生 changelog
輕輕鬆鬆產生 changelog
 
Speed up add custom marker on google map
Speed up add custom marker on google mapSpeed up add custom marker on google map
Speed up add custom marker on google map
 
Spek
SpekSpek
Spek
 
Jenkins for android developer at TWJUG
Jenkins for android developer at TWJUGJenkins for android developer at TWJUG
Jenkins for android developer at TWJUG
 
自己的 Jenkins 自己來 for Android developer
自己的 Jenkins 自己來  for Android developer自己的 Jenkins 自己來  for Android developer
自己的 Jenkins 自己來 for Android developer
 
從開發到上線的華麗大冒險
從開發到上線的華麗大冒險從開發到上線的華麗大冒險
從開發到上線的華麗大冒險
 
Unit test and ui testing with cucumber
Unit test and ui testing with cucumberUnit test and ui testing with cucumber
Unit test and ui testing with cucumber
 
RxJava With retrolambda
RxJava With retrolambdaRxJava With retrolambda
RxJava With retrolambda
 
ORMLite Android
ORMLite AndroidORMLite Android
ORMLite Android
 
設計師合作經驗分享
設計師合作經驗分享設計師合作經驗分享
設計師合作經驗分享
 
Dog point
Dog pointDog point
Dog point
 
Gson
GsonGson
Gson
 
Hybrid design with bootstrap
Hybrid design with bootstrapHybrid design with bootstrap
Hybrid design with bootstrap
 

Recently uploaded

Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
ossaicprecious19
 
FAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable PredictionsFAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable Predictions
Michel Dumontier
 
The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...
Health Advances
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
Areesha Ahmad
 
Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
Cherry
 
Predicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdfPredicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdf
binhminhvu04
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
muralinath2
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
Areesha Ahmad
 
Hemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptxHemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptx
muralinath2
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
silvermistyshot
 
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdfUnveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Erdal Coalmaker
 
insect morphology and physiology of insect
insect morphology and physiology of insectinsect morphology and physiology of insect
insect morphology and physiology of insect
anitaento25
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
subedisuryaofficial
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
yusufzako14
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
SAMIR PANDA
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
sachin783648
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
YOGESH DOGRA
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
Scintica Instrumentation
 
Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
AADYARAJPANDEY1
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
Sérgio Sacani
 

Recently uploaded (20)

Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
 
FAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable PredictionsFAIR & AI Ready KGs for Explainable Predictions
FAIR & AI Ready KGs for Explainable Predictions
 
The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...The ASGCT Annual Meeting was packed with exciting progress in the field advan...
The ASGCT Annual Meeting was packed with exciting progress in the field advan...
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
 
Large scale production of streptomycin.pptx
Large scale production of streptomycin.pptxLarge scale production of streptomycin.pptx
Large scale production of streptomycin.pptx
 
Predicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdfPredicting property prices with machine learning algorithms.pdf
Predicting property prices with machine learning algorithms.pdf
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
 
GBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram StainingGBSN- Microbiology (Lab 3) Gram Staining
GBSN- Microbiology (Lab 3) Gram Staining
 
Hemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptxHemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptx
 
Lateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensiveLateral Ventricles.pdf very easy good diagrams comprehensive
Lateral Ventricles.pdf very easy good diagrams comprehensive
 
Unveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdfUnveiling the Energy Potential of Marshmallow Deposits.pdf
Unveiling the Energy Potential of Marshmallow Deposits.pdf
 
insect morphology and physiology of insect
insect morphology and physiology of insectinsect morphology and physiology of insect
insect morphology and physiology of insect
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
 
plant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptxplant biotechnology Lecture note ppt.pptx
plant biotechnology Lecture note ppt.pptx
 
Seminar of U.V. Spectroscopy by SAMIR PANDA
 Seminar of U.V. Spectroscopy by SAMIR PANDA Seminar of U.V. Spectroscopy by SAMIR PANDA
Seminar of U.V. Spectroscopy by SAMIR PANDA
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
 
Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
 

Kotlin初體驗

  • 2. @2017 PKLOT CO., LTD ABOUT ME ▸ Andy ▸ @ 停⾞車車⼤大聲公 ▸ @ Android Developer 讀書會 (TADSG) ▸ @ Android Code Club (週三晚上)
  • 3. @2017 PKLOT CO., LTD 有⼈人聽過 KOTLIN 嗎?
  • 4. @2017 PKLOT CO., LTD 有⼈人⽤用過 KOTLIN 嗎?
  • 5. @2017 PKLOT CO., LTD HELLO KOTLIN ▸ JetBrains 開發的 JVM 語⾔言 ▸ 包含很多 Effective Java 的思想在裡⾯面 ▸ Java 開發者的夢幻語⾔言 ?!
  • 6. @2017 PKLOT CO., LTD WHY KOTLIN ▸ Short Code ▸ Null Safety ▸ Smart Cast ▸ Lambda (Functional Programing) ▸ Method Extension ▸ Hybrid with Java ▸ Default final ▸ Everything is Object
  • 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” )
  • 20. @2017 PKLOT CO., LTD LAMBDA ▸ 不必再等 Java 8 到天荒地老 ▸ 不⽤用 Retrolambda ▸ Kotlin 內建 lambda
  • 21. @2017 PKLOT CO., LTD LAMBDA ▸ setOnClickListener() view.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Toast.makeToast(…..).show() } })
  • 22. @2017 PKLOT CO., LTD LAMBDA ▸ setOnClickListener() view.setOnClickListener({ view -> Toast.makeToast(…..).show() }) view.setOnClickListener({ Toast.makeToast(…..).show() })
  • 23. @2017 PKLOT CO., LTD LAMBDA ▸ setOnClickListener() view.setOnClickListener{ Toast.makeToast(…..).show()} view.onClick{ Toast.makeToast(…..).show()}
  • 24. @2017 PKLOT CO., LTD LAMBDA ▸ Adapter OnItemClickListener interface OnOrderItemClickListener{ onOrderItemClick(Order order); } OnOrderItemClickListener onOrderItemClickListener; ‣ interface, setter, init, invoke
  • 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()
  • 29. @2017 PKLOT CO., LTD PROPERTISE DELEGATE ‣ Demo Delegate SharedPreferences
  • 30. @2017 PKLOT CO., LTD DEPENDENCY INJECTION ‣ In MPV Pattern ‣ Presenter need inject Model ‣ Repository(NetworkService networkService, LocalData localData) ‣ LocalData(Content context) ‣ Presenter(View view, Repository repository)
  • 31. @2017 PKLOT CO., LTD DEPENDENCY INJECTION ‣ new Presenter ‣ In Java Presenter presenter = new Presenter(new Repository(new NetworkService(), new LocalData(context))); // so ugly Presenter presenter = PresenterFactory.create();
  • 32. @2017 PKLOT CO., LTD DEPENDENCY INJECTION ‣ In kotlin ‣ Repository(networkService : NetworkService = NetworkService(), localData : LocalData = LocalData()) ‣ LocalData(context : Content = App.instance()) ‣ Presenter(view : View, repository : Repository = Repository())
  • 33. @2017 PKLOT CO., LTD DEPENDENCY INJECTION ‣ In kotlin ‣ val presenter = Presenter(view) ‣ for testing ‣ val presenter = Presenter(view, mockRepository)
  • 34. @2017 PKLOT CO., LTD REFERENCE ▸ Kotlin official guide ▸ https://kotlinlang.org/docs/reference/ ▸ Kotlin For Android Developer (e-book) ▸ https://antonioleiva.com/kotlin-android-developers-book/
  • 35. @2017 PKLOT CO., LTD 學習⼀一⾨門語⾔言最快的⽅方式就是只能⽤用它!
  • 36. @2017 PKLOT CO., LTD Q&A @2017 PKLOT CO., LTD