Kotlin basic study notes

1. Basic data types

/**
 * Author: TianMing.Xiong
 * Date: Created in 17-12-23 下午6:12
 */
fun main(args: Array<String>) {

    //8 basic data type declarations: var|val variable name: [type]=initial value
    var aBoolean = false
    var aChar='a'

    var aByte:Byte= Byte.MAX_VALUE
    var aShort:Short= Short.MAX_VALUE
    var aInt:Int= Int.MAX_VALUE
    var aLong=1L

    var aFloat = 2F
    val aDouble=3.0

    //reference type
    val aString="Hello"

    println("$aBoolean")
    println("$aChar")
    println("$aByte")
    println("$aShort")
    println("$aInt")
    println("$aLong")
    println("$aFloat")
    println("$aDouble")
    println("$aString")
}

output:

false
a
127
32767
2147483647
1
2.0
3.0
Hello

2. Function method definition

package com.xtm.demo

fun main(args: Array<String>) {
    println("a+b=${sum(10,20)}")
    println("a+b=${sum1(19,1)}")
    println("a+b=${sum2(19,2)}")
    printSum(12,13)

}

/**
 * function with no return value
 */
fun printSum(a:Int,b:Int){
    println("void print:"+(a+b))
}

/**
 * function with return value
 * Format: fun function name(param1:Type1,param2:Type2...):Type
 */
fun sum(a: Int, b: Int):Int{
    return a+b
}

/**
 * Automatic conversion function
 */
fun sum1(a:Int,b:Int) = a + b

/**
 * Function shorthand
 */
fun sum2(a:Int,b:Int):Int = a+b

output:

a+b=30
a+b=20
a+b=21
void print:25

3. Classes and abstract classes and interfaces

package com.xtm.demo


/**
 * Author: TianMing.Xiong
 * Date: Created in 17-12-23 上午9:30
 */
fun main(args: Array<String>){

    // normal class
    val site = WebSit ()
    site.name="www.baidu.com"
    site.city="shenzhen"
    println(site.toString())
    site.test()
    //Construction method
    val animal = Animal("bird",2)
    animal.test()
    val animal1 = Animal("熊猫", 3, "white")
    animal1.test()

    // abstract method
    val sayHiImp = SayHiImp ()
    sayHiImp.sayHi("Tom")

    //interface
    val sayHelloImp = SayHelloImp()
    sayHelloImp.sayHello("Lily")


}

//Ordinary class setter and getter
class WebSit {

    var name: String=""
    get() = field
    set(value) {
        field=value
    }

    var city: String=""
    get() = field
    set(value) {
        field=value
    }

    fun test(){
        println("test...\n")
    }

    override fun toString(): String {
        return "name:${this.name}\ncity:${this.city}"
    }
}


//Construction method
class Animal constructor(private var name: String, private var age: Int){//Main constructor
    private var color:String="gray"
    constructor(name: String,age: Int,color: String):this(name,age){//Secondary constructor
        this.name=name
        this.age=age
        this.color=color
    }
    init {
        println("init...")

    }

    fun test(){
        println("name:${this.name },age:${this.age},color:${this.color}\n")
    }
}

// abstract class
open class BaseSayHi{
    open fun sayHi(name:String){}
}

abstract class AbsSayHi:BaseSayHi() {
    override abstract fun sayHi(name:String)
}

class SayHiImp :AbsSayHi(){
    override fun sayHi(name:String) {
        println("hi!  $name\n")
    }

}

//interface
interface ISayHello{
    fun sayHello(name:String)
}
class SayHelloImp :ISayHello{
    override fun sayHello(name: String) {
        println("Hello ! $name")
    }

}



output:

name:www.baidu.com
city:shenzhen
test...

init...
name: bird, age: 2, color: gray

init...
name:熊猫,age:3,color:white

hi! Tom

Hello ! Lily



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894709&siteId=291194637