Summary of some knowledge points in Kotlin language

function passed as parameter

    //创建一个方法,该方法传递的是一个函数 f,Unit表示该函数函数不需要返回任何有效值
    fun uiThread(f:()->Unit){
        handler.post { f() }
    }
//下面这种和上面达到的小姑是相同的;都是handler执行f方法体
    fun uiThreadMethod(f:()->Unit){
        handler.post(f)
    }

Widely used by lazy and object comparison

   companion object {
        val handler by lazy {
            Handler(Looper.getMainLooper())
        }
    }

    //函数
    fun uiThread(f:()->Unit){
        handler.post { f() }
    }
  • by lazy is used to initialize the objects and instances that are currently needed; decorated with val
  • Use the static keyword in java to modify static methods. Static methods can be called in the form of class name. Method name [this is obvious in the singleton]
  • In kotlin, object is used to modify static classes , and the modified class can call methods in this class by means of class name and method name.
  • Comapnion object is used in kotlin to decorate static methods , which can be called by class name.method name

Guess you like

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