Kotlin Tutorial (part 1)
I haven’t summarized my work and study for a long time, so I want to use Kotlin programming for a simple summary in the next month. First of all, Kotlin is really easy to use, and in Android development, you can see the convenience and flexibility kotlin brings. Java programmers can make a very friendly transition to Kotlin. Here I’ll focus on Android.
1) introduce the kotlin
AS3.0 has great support for Kotlin. When you create a new.kt file, the libraries that Kotlin relies on will almost always come in. The module build. Gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Copy the code
In the project build. Gradle
dependencies {
.....
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Copy the code
2) Basic grammar
- Basic types of In. Basic types of kt is similar to the packaging Java types, such as: Int, Long, String, Boolean… Note that when there is no “?” These objects are not null when they are declared, which is where Kotlin can kill the NPE the most; 2) define a function with no return value eg:
fun sum(a:Int ,b:Int){
println("$a plus $b = ${a+b}")
}
or
fun sum(a:Int ,b:Int):Unit{
println("$a plus $b = ${a+b}")}Copy the code
Returns eg:
fun sum(a:Int ,b:Int):Int{
returnFun sum(a:Int,b:Int):Int? Fun sum(a:Int,b:Int):Int? {returnNull} or fun sum(a:Int,b:Int)=a+bCopy the code
- Expressions such as if,for,when,rang
ifStatements fun maxOf = (a: Int, b: Int)if(a>b) a elseB The above functions are similar to Java ternary operation; Kotlin can omit the return value type declaration when the type is inferred;forStatement val fruits = arrayOf("apple"."banana"."kiwi")
for(fruit inFruits){println(fruit)} or fruits. ForEach {println(it)}} val fruits = arrayOf("apple"."banana"."kiwi")
when {
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
}
or
fruits.forEach {
when (it) {
"apple"."banana" -> println("I love $it")
else -> println("I don't love $it"}} rang statement val x=520;if(x in1.. 521){ println("$x in range")}Copy the code
4) Interval iteration eg:
Print 1,3,5; Step can be omitted if step is 1for(x in1.. 5 step 2){ println(x) } orfor(x in 5 downTo 1 step2){
println(x)
}
Copy the code
There are many other basic uses that are not listed, but refer to the official Kotlin documentation
3) Create classes and instances
1) Unlike Java, Kotlin defaults to classes that cannot be integrated. Classes that can be inherited need to be explicitly decorated with open;
Open class Foo{} Sub class:FooOpen class Foo(p:String?) {// Foo(p:String?); { funrun1(){
println("invoke run1 in Foo")
}
open fun run2(){
println("invoke run2 in Foo")
}
}
Sub(p:String?) class:Foo(p){
override fun run2() {super.run2()} // The superclass method fun is not overridden hererun1(){
println("invoke run1 in Sub"In kotlin, new objects are created without the new keyword; Similarly, functions need the open modifier to be overridden by subclasses. val foo= foo ("google Jang") foo.run1().apply{run2()} // Print: invoke run1in Foo
invoke run2 in Foo
Copy the code
Time is limited, so I’ll write that first, and the next article will focus on lambda expressions and extension functions