Kotlin 학습 첫날 (기본 문법)

5491 단어
참고 자료https://kymjs.com/code/2017/02/04/01/https://github.com/youxin11544/Kotlin-learning https://www.jianshu.com/p/1ea733ea197d
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //1.  
        var  quantity = 5
        var price:Double = 20.3
        var name:String = "  "
        Log.i("aaa","  :$price")
        Log.i("aaa","  :$quantity")
        Log.i("aaa","  :$name   :${quantity*price}")
        //2.let    ,    ?.   
        var data = null
        data?.let {
            // data      
            Log.i("aaa","data   ")
        }
        data?:let {
            // data      
            Log.i("aaa","data   ")
        }

        getStringLength("20")
        lateinit()
    }
    //3.      
    fun getStringLength(obj:String):Int?{
        if (obj is String){
            //obj         String  
            return obj.length
        }
        if (obj !is String){
            //  !is,   ,  String  
        }
        //            obj   String     ,        obj Int  。
        return null
    }
    //4.     ,   fun  ,  say()   ,    String     ,     String    
    fun say(str:String):String{
        return str
    }
    //5.  ,  Kotlin  ,          ,     
    fun says(str:String):String = str
    //6.         (                ),               
    fun getInValue(value:Int) = value
    fun getLength(str: String) = str.length
    fun getStr(str: String) = str
    fun getLong(str: String) = 10L
    fun getDouble(str: String) = 10.3
    //7.                  (7.1  say(),         "hello",7.2         say("world")        )
    fun sayss(str: String = "hello"):String = str
    //8.        ,           ,      .
    fun saysss(firstName:String = "tou",lastName:String ="wei"){}
    //9.        ,  true false
    fun hasEmpty(vararg strArray: String?):Boolean{
        for (str in strArray){
            if ("".equals(str) || str == null)
                return true
        }
        return false
    }

    fun lateinit(){
        //10.    int[][][] arrs = new int[3][2][1];
        val arrs = Array(3){ Array(2){IntArray(1)}}
        //11.     
        var ltstr:String = "hello"
        var ltstrs = "word"
        //12.        
        var ltstrss:String?=null
        //13.val    var      
        val myname: String = "tutu"
        //14.      
        var myage = "23"
        //15.fun    
        fun getName(): String{
            return "tutu"
        }
        //16.   
        class UserList(): ArrayList() {}
        //17.bean   
        class User {
            var names: String? = null
            var ages: String? = null
        }
        //18.  bean   
        val user = User()
        //  
        user.names = "tutu"
        user.ages = "23"
        //  
        val namess = user.names
        val agess = user.ages
        //19.     
        var userInfo = "name:${user.names},  age:${user.ages}"
        //    :name:tutu, age:23
        //20.   
        //20.1       ?     
        var age: String? = "23"
        //20.2         
        val ages = age!!.toInt()
        //20.3         null
        val ages1 = age?.toInt()
        //20.4  age    -1
        val ages2 = age?.toInt() ?: -1
        //21.if...else as    ,   switch,  when  
        //when       、  、        、  Boolean     ,    if...else if
        val x = 7
        when (x) {
            in 1..10 -> Log.i("aaa","x 1 10  ")
            !in 10..20 -> Log.i("aaa","x 10 20  ")
            else -> Log.i("aaa","     ")
        }
        // 22.for  
        val list = ArrayList()
        //22.1  for (int i = 0; i < list.size(); i++)
        for (i in list.indices) {
            print(list[i])
        }
        //22.2  for (int i = list.size(); i >= 0; i--)
        for (i in list.size downTo 0) {
            print(list[i])
        }
        //22.3        
        for (item in list) {
            print(item)
        }
        //22.4   
        list.forEach {
            print(it)
        }
        //23.1    
        var a: String = "abc"
        a = null // compilation error,    
        //23.2   (    )
        var b: String? = "abc"
        b = null // ok,    
        //24.Elvis        ?.                      ,              Int?
        var c: String? = "abc"
        val num = c?.length
        //b?.length ?: -1   if (b != null) b.length else -1      。
        //25.       !!                ,           ;      NPE,    NPE,          
        //26.orElse  , Elvis      ,         null
        var a:String? = null
        var b = a ?: orElse {
            //        ,         
            return@orElse "s"
        }
        var a:String? = null
        var b = a ?: orElse {
            //       ,   return    throw          
            return
        }
        //Elvis                      
        //27.guard                   
        fun testGuard(a: String?, b: String?, c: String?){
            guard(a, b, c) ?: orElse {
                print("a,b,c   ")
                return
            }
            print("a,b,c    ")
        }
    }
 }

좋은 웹페이지 즐겨찾기