Kotlin let、with、run、apply、also函数的使用

let,with,run,apply,also 是内联扩展函数

下面是自己使用的心的如果有错的地方希望给予指正谢谢

这几个主要用来简化操作,使得代码可读性提高 ,下面列举项目中使用效果

1 let 

 先不啰嗦了 直接代码吧 ,

不是let  的代码

 val list = ArrayList()
        list.add("A")
        list.add("B")
        list.add("C")
        for (i in list.indices) {
            println(list[i])
        }

使用let 之后如下

         val list = ArrayList()
         list.let {
             it.add("A")
             it.add("B")
             it.add("C")
         }
         for (i in list.indices) {
             println(list[i])
             Log.e("==============hly=======",list[i])
         }

他们对比发现list 没有反复的使用, 不过却有一个it 

自己在项目中测试let也使用了

没有使用let 如下 

这个是刷新的地方, 估计很多人都在使用,是不是刷新的id要写2次

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)

        smart.setOnRefreshListener { this@MainActivity }
        smart.setOnLoadMoreListener { this@MainActivity }

使用let如下

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)

        smart.let {
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }

刚才是自己点击let看看源码

/**
 * Calls the specified function [block] with `this` value as its argument and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun  T.let(block: (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block(this)
}

百度翻译下 let 注释 

以' this '值作为参数调用指定的函数[block]并返回其结果。

 

2 with

还是用刚才的list代码 用with 试试

   val list = ArrayList()
          with(list){
              add("A")
              add("B")
              add("C")
          }
         for (i in list.indices) {
             println(list[i])
             Log.e("==============hly=======",list[i])
         }

这个写法和刚才的有很大差别是不是

在使用with 实现刷新

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //原始的
        smart.setOnRefreshListener { this@MainActivity }
        smart.setOnLoadMoreListener { this@MainActivity }
        //let
        smart.let {
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }
        //with
        with(smart) {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

 

with 的源码

 

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun  with(receiver: T, block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return receiver.block()
}

翻译下with的注释:

使用给定的[receiver]调用指定的函数[block]并返回其结果。

 

3 run 

使用刚才的list 如下

 val list = ArrayList()
            list.run {
                add("A")
                add("B")
                add("C")
            }
        for (i in list.indices) {
            println(list[i])
            Log.e("==============hly=======", list[i])
        }

在写一些刷新看看

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //原始的
        smart.setOnRefreshListener { this@MainActivity }
        smart.setOnLoadMoreListener { this@MainActivity }
        //let
        smart.let {
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }
        //with
        with(smart) {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }
        
        //run
        smart.run {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

 

这些我没把let ,with 去掉方便大家对比查看,实现的效果是一样的

 

run的源码

/**
 * Calls the specified function [block] with `this` value as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun  T.run(block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}

翻译注释

调用指定的函数[block],并以' this '值作为其接收方,返回其结果。

4 apply

刚开的list 使用apply 如下

 val list = ArrayList()
            list.apply {
                add("A")
                add("B")
                add("C")
            }
        for (i in list.indices) {
            println(list[i])
            Log.e("==============hly=======", list[i])
        }

apply 实现的刷新 如下

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //原始的
        smart.setOnRefreshListener { this@MainActivity }
        smart.setOnLoadMoreListener { this@MainActivity }
        //let
        smart.let {
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }
        //with
        with(smart) {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

        //run
        smart.run {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

        //apply
        smart.apply{
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

apply的源码

/**
 * Calls the specified function [block] with `this` value as its receiver and returns `this` value.
 */
@kotlin.internal.InlineOnly
public inline fun  T.apply(block: T.() -> Unit): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
    return this
}

翻译注释

调用指定的函数[block],并将' this '值作为其接收器,返回' this '值。

5 also

使用刚才的list also 的使用如下

  val list = ArrayList()
            list.also {
                it.add("A")
                it.add("B")
                it.add("C")
            }
        for (i in list.indices) {
            println(list[i])
            Log.e("==============hly=======", list[i])
        }

 

also 的刷新如下

class MainActivity : AppCompatActivity(), OnRefreshListener, OnLoadMoreListener {

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    @SuppressLint("LongLogTag")
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //原始的
        smart.setOnRefreshListener { this@MainActivity }
        smart.setOnLoadMoreListener { this@MainActivity }
        //let
        smart.let {
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }
        //with
        with(smart) {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

        //run
        smart.run {
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

        //apply
        smart.apply{
            setOnRefreshListener { this@MainActivity }
            setOnLoadMoreListener { this@MainActivity }
        }

        //also
        smart.also{
            it.setOnRefreshListener { this@MainActivity }
            it.setOnLoadMoreListener { this@MainActivity }
        }

这个几个写法和对比都在这里, 我自己都一一测试过的,局可以实现 ,

以上就是他们的使用方法 , 有不明白的或者错误的希望指点一下,相互进步.....

 

你可能感兴趣的:(Kotlin,Kotlin知识总结)