【问题标题】:it[imageView, uri, placeHolder] = tag ,what this mean?it[imageView, uri, placeHolder] = tag ,这是什么意思?
【发布时间】:2022-01-22 21:50:37
【问题描述】:

在阅读MaterialDrawerKt库的源代码时,有些代码我看不懂?谁能给我解释一下?谢谢

/**
    * @param imageView
    * @param uri
    * @param tag
    * @return false if not consumed
    */
   open fun setImage(imageView: ImageView, uri: Uri, tag: String?): Boolean {
       // If we do not handle this protocol we keep the original behavior
       return if (mHandleAllProtocols || uri.scheme in mHandledProtocols) {
           imageLoader?.let {
               val placeHolder = it.placeholder(imageView.context, tag)
               it[imageView, uri, placeHolder] = tag
           }
           true
       } else false
   }

谁能解释一下这段代码?it[imageView, uri, placeHolder] = tag

【问题讨论】:

  • 当你看到这样的括号时,它相当于用这些参数调用了一个名为get()set()的函数,因此代码与it.set(imageView, uri, placeholder, tag)相同
  • 是的,你是对的。你解决了我的问题。谢谢

标签: java android kotlin syntax


【解决方案1】:

这叫做indexed access operator

(从链接复制):

Expression Translated to
a[i] a.get(i)
a[i, j] a.get(i, j)
a[i_1, ..., i_n] a.get(i_1, ..., i_n)
a[i] = b a.set(i, b)
a[i, j] = b a.set(i, j, b)
a[i_1, ..., i_n] = b a.set(i_1, ..., i_n, b)

方括号被转换为使用适当数量的参数获取和设置的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2013-08-17
    • 2011-09-04
    相关资源
    最近更新 更多