欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-01-06 08:09:04
...

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

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

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

1 let 

 先不啰嗦了 直接代码吧 ,

不是let  的代码

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

使用let 之后如下

         val list = ArrayList<String>()
         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 { [email protected] }
        smart.setOnLoadMoreListener { [email protected] }

使用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 { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }

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

/**
 * Calls the specified function [block] with `this` value as its argument and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> 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<String>()
          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 { [email protected] }
        smart.setOnLoadMoreListener { [email protected] }
        //let
        smart.let {
            it.setOnRefreshListener { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }
        //with
        with(smart) {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

 

with 的源码

 

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> 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<String>()
            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 { [email protected] }
        smart.setOnLoadMoreListener { [email protected] }
        //let
        smart.let {
            it.setOnRefreshListener { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }
        //with
        with(smart) {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }
        
        //run
        smart.run {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

 

这些我没把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, R> T.run(block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}

翻译注释

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

4 apply

刚开的list 使用apply 如下

 val list = ArrayList<String>()
            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 { [email protected] }
        smart.setOnLoadMoreListener { [email protected] }
        //let
        smart.let {
            it.setOnRefreshListener { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }
        //with
        with(smart) {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

        //run
        smart.run {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

        //apply
        smart.apply{
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

apply的源码

/**
 * Calls the specified function [block] with `this` value as its receiver and returns `this` value.
 */
@kotlin.internal.InlineOnly
public inline fun <T> 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<String>()
            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 { [email protected] }
        smart.setOnLoadMoreListener { [email protected] }
        //let
        smart.let {
            it.setOnRefreshListener { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }
        //with
        with(smart) {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

        //run
        smart.run {
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

        //apply
        smart.apply{
            setOnRefreshListener { [email protected] }
            setOnLoadMoreListener { [email protected] }
        }

        //also
        smart.also{
            it.setOnRefreshListener { [email protected] }
            it.setOnLoadMoreListener { [email protected] }
        }

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

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

 

相关标签: let apply

上一篇:

下一篇: