当前位置: 首页>后端>正文

kotlin快速入门

        //变量
        val a = 1   //        val:value,表示一个不可变的值或者只读变量。     final int a = 1;
        val a2:Int = 1;
        var b = 2   //        var:variable,表示一个可变的值或者可读写变量。   int = 2;
        const val Max = 200 //预编译常量使用const

        //空安全判断
/*      String str = null;
        if (str != null) {
            int length = str.length();
        }*/
        val str: String= null
        val length = str?.length

        //数组
        val names: Array<String> = arrayOf("Kyo", "Ryu", "Iory") //String[] names = new String[]{"Kyo", "Ryu", "Iory"};
        val emptyStrings: Array<String?> = arrayOfNulls(10) //String[] emptyStrings = new String[10];

        //函数
        // 修饰符  关键字 函数名(参数:参数类型,参数2:参数2类型):返回值类型 {}
        private fun doSomething(a: Int, b: Int = 2): String {
            println("函数默认值:$b")
            return a.toString()
        }

        //三元运算符
        Int code = if(isSuccessfully) 200 else 400 //int code = isSuccessfully200: 400;

        //如何实例化类
        val date = Date() // Date date = new Date();

        //匿名函数跟java8中的类似
        fun main() {
            showOnBoard("卫生纸"){goodsName:String, hour:Int ->
                val currentYear = 2027
                "${currentYear}年,双11${goodsName}促销倒计时:$hour 小时"
            }
        }
        //具名函数
        private fun showOnBoard(goodsName:String, getDiscountWords: (String,Int) -> String){
            val hour =4;
            println(getDiscountWords(goodsName,hour))
        }
        //LIST
        //    不可变列表
        val list = listOf(1, 2, 3, 4, 5)
        println(list[2])
        println(list.getOrElse(38
        ){"Unknown"})
        //    可变列表
        val mutableList = mutableListOf(1, 2, 3, 4)
        mutableList.addAll(list)
        mutableList.remove(1)  // remove element ,not is index
        //相互转换
        val list1 = mutableList.toList()
        val toMutableList = list.toMutableList()

        //遍历
        val list = listOf("Jason", "Jack", "Jacky")
        for (s in list) {
            println(s)
        }
        list.forEach {
            println(it)
        }
        list.forEachIndexed { index, item ->
            println("$index, $item")
        }
        val (origin,_,proxy)  = list

        // map
        val map = mapOf("a" to "b", "c" to "d")
        println(map["a"])
        println(map.getValue("a"))
        println(map.getOrElse("a") { "undefined" })
        println(map.getOrDefault("a") { "ab" })
        for (entry in map) {
            println(entry.key + " " + entry.value)
        }
        map.forEach {
            println(it.key + "_" + it.value)
        }
        map.forEach { (k, v) ->
            println("$k,$v")
        }
        val mutableMap = map.toMutableMap()
        mutableMap += "e" to "f"
        mutableMap["i"] = "j"
        mutableMap.put("g","h")
        mutableMap.getOrPut("k"){"l"}

        //如何写 Getter 和 Setter 方法
//        class GetterAndSetter {
//            private var x = 0
//            fun getX(): Int {
//                return x
//            }
//
//            fun setX(x: Int) {
//                this.x = x
//            }
//        }
        class KotlinGetterAndSetter{
            var x: Int = 0
                set(value) { field = value }
                get() = field
        }

        //

https://www.xamrdz.com/backend/3e21924785.html

相关文章: