kotlin note 1

昨天的贴一下
我打算以十二生肖为包名,建立kotlin的入门demo库

//the options set pakcage
package mouse

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:05
 */
/**
 * this function opened to the package visible
 * it takes a String array (type) as parameters named: args (call name)
 */
fun main(args: Array<String>) {
    //the ; can not write behind ), it's different between java & kotlin
    println("Hello World!")
}


package mouse

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:15
 */
//this param is String called name , using val
class Greeter(val name:String){
    fun greet(){
        println("Hello,$name")
    }
}

fun main(){
    //create an object dont using "new" XXX
    Greeter("World").greet()
}


/*为什么选择 Kotlin?
简洁: 大大减少样板代码的数量。
安全: 避免空指针异常等整个类的错误。
互操作性: 充分利用 JVM、Android 和浏览器的现有库。
工具友好: 可用任何 Java IDE 或者使用命令行构建。*/

package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:24
 */
/**
 * the params a is a int, b is a int,return a int type
 */
fun sum(a:Int,b:Int):Int{
    return a+b
}

/**
 * functional fun the return type is automated
 */
fun sum1(a:Int,b:Int) = a+b

/**
 * the public fun must be use return type :Int or some else
 */
public fun sum2(a:Int,b:Int):Int = a+b

/**
 * Unit means void in java , no return type
 */
fun printSum(a:Int,b:Int):Unit{
    print(a+b)
}

/**
 * if return type is Unit,
 * then can ignore write that,
 * also the public fun
 */
public fun printSum2(a:Int,b:Int){
    print(a+b)
}

fun main(){
    println(sum(1,2))
    println(sum1(3,4))
    println(sum2(5,6))

    printSum(1,3)
    println()
    printSum2(1,4)
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:36
 */
/**
 * It can be using 'vararg' set more parameters
 * Just like the java (String... args)
 */
fun vars(vararg v:Int){
    for(vt in v){
        print(vt)
    }
}

fun main(args: Array<String>){
    vars(1,2,3,4,5,6)
    //print 123456
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:39
 */
//using lambda to do Math
fun main(){
    /**
     * val one sumLambda: get two params Int,Int -> then
     * response Int type value, this value is the x,y->x+y make sum
     * {x,y->x+y}
     */
    val sumLambda: (Int,Int) -> Int = {x,y -> x+y}
    /**
     * so you can saw 3
     */
    println(sumLambda(1,2))
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 21:59
 */
fun variables(){
    val a: Int = 1
    val b = 1
    //be declare then asset
    var c: Int
    c = 1

    var x = 5
    x += 1
    println(a)
    println(b)
    println(c)

    println(x)
}

fun main(){
   variables()
    /**
     * 1
     * 1
     * 1
     * 6
     */
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 22:56
 */
//just like java
fun singleAnnotation(){
    //single annotation

    /*val hello:(String) -> Unit = {x->x+" ~~~"}
    * the block annotation,it can be log kotlin.Unit */
    val hello:(String) -> String = {x->x+" ~~~"}
    print(hello("你好呀"))
}

fun main(){
    singleAnnotation()
    //single annotation,sysout: the variables "你好呀 ~~~"from lambda val
}




package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-29 23:06
 */
/**
 * string template , 
 * using 'replace' replacing that string template will create new one
 * 
 */
fun template(){
    var a = 1
    //simple name in template
    val s1 = "a is $a"

    a = 2
    //any expression in template
    val s2 = "${s1.replace("is","was")},but now is $a"

    println(s1)

    println(s2)

}

fun main(){
    template()
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-30 15:04
 */


fun checkNull(){
    //the tyoe String and ? means the variable can be null
    var age: String? = "23"
//    var age: String?  //if not initialize, the age!!.toInt() throws Error must be initialized
    //throw exception
    val ages = age!!.toInt()
    //return null
    val ages1 = age?.toInt()
    //if age is null return -1
    val ages2 = age?.toInt() ?: -1

    println(age)
    println(ages)
    println(ages1)
    println(ages2)
}

fun main(){
    checkNull()
}


package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-30 15:04
 */


fun checkNull() {
    //the tyoe String and ? means the variable can be null
    var age: String? = "23"
//    var age: String?  //if not initialize, the age!!.toInt() throws Error must be initialized
    //throw exception
    val ages = age!!.toInt()
    //return null
    val ages1 = age?.toInt()
    //if age is null return -1
    val ages2 = age?.toInt() ?: -1

    println(age)
    println(ages)
    println(ages1)
    println(ages2)
}

fun main() {
    checkNull()
    var ele1 = "123"
    var ele2 = "412"
    //make a array using arrayOf(element,element1,...)
    example1(arrayOf(ele1, ele2))
}

//two String params check and parseInt multiply
fun example1(params: Array<String>) {
    if (params.size < 2) {
        println("Two params expected")
        return
    }
    //lambda string -> int 
    //toInt() : public actual inline fun String.toInt(): Int = java.lang.Integer.parseInt(this) using java function
    val parseInt: (String) -> Int = { x -> x.toInt() }

    val x = parseInt(params[0])
    val y = parseInt(params[1])

    if (x != null && y != null) {
        println(x * y)
    }
}
package cow

/**
 * @Author: zhangQi
 * @Date: 2020-08-30 15:58
 */
/**
 * getStringLength, but the convert to String is automatic
 */
fun getStringLength(obj: Any): Int? {
    //judgement the type of data : Any
    if (obj is String) {
        return obj.length
    } else if (obj !is String) {
        println("obj is not String")
    } else if (obj is Int) {
        println("obj is Int, get length of obj : Int")
        return obj.length
    } else {
        println("other type don't return length~")
        return null
    }
    return null
}

fun getStringLength1(obj: Any): Int? {
    if(obj is String){
        return obj.length
    }
    return null
}

fun getStringLength2(obj: Any):Int?{
    if(obj !is String){
        return null
    }
    return obj.length
}

fun main(args:Array<String>){
    //变量永远不会被修改,可以使用“val”声明为不可变变量
    val str1 :String = "www.cnblogs.com/ukzq"
    val l1 = getStringLength(str1)
    println(l1)

    val l2 = getStringLength1(str1)
    println(l2)

    val l3 = getStringLength2(str1)
    println(l3)

    val test2 :Long = 20160404
    getStringLength(test2)
    getStringLength1(test2)
    getStringLength2(test2)
}

https://www.runoob.com/kotlin/kotlin-basic-syntax.html

代码仓库
https://github.com/deadzq/kotlin-repo

posted @ 2020-08-30 16:18  ukyo--夜王  阅读(157)  评论(0编辑  收藏  举报