<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### [`this`關鍵字](http://www.kotlincn.net/docs/reference/this-expressions.html) [TOC] `this`關鍵字持有當前對象的引用。我們可以使用`this`來引用變量或者成員函數,也可以使用`return this`,來返回某個類的引用。 代碼示例 ~~~ fun main(args: Array<String>) { val demo = ThisDemo() println(demo.whatIsThis()) } class ThisDemo { val thisis = "THIS IS" fun whatIsThis(): ThisDemo { println(this.thisis) //引用變量 this.howIsThis()// 引用成員函數 return this // 返回此類的引用 } fun howIsThis(){ println("HOW IS THIS ?") } } ~~~ 運行結果 ``` THIS IS HOW IS THIS ? A基礎.ThisDemo@5e481248 ``` ### This 表達式 為了表示當前的接收者我們使用this表達式: * 在[類](http://www.kotlincn.net/docs/reference/classes.html#繼承)的成員中,this指的是該類的當前對象。 * 在[擴展函數](http://www.kotlincn.net/docs/reference/extensions.html)或者[帶有接收者的函數字面值](http://www.kotlincn.net/docs/reference/lambdas.html#帶有接收者的函數字面值)中,this表示在點左側傳遞的接收者參數。 ~~~ fun main(args: Array<String>) { //在擴展函數或者帶接收者的函數字面值中, this 表示在點左側傳遞的接收者參數。 val sum= fun Int.(x:Int):Int = this +x println("sum: ${sum}") println("1.sum(1):${1.sum(1)}") val concat =fun String.(x:Any) = this + x val str="abc" println("str:${str}") println("str.concat(123):${str.concat(123)}") println("str.concat(true):${str.concat(true)}") } ~~~ 運行結果 ``` sum: kotlin.Int.(kotlin.Int) -> kotlin.Int 1.sum(1):2 str:abc str.concat(123):abc123 str.concat(true):abctrue Process finished with exit code 0 ``` 如果this沒有限定符,它指的是最內層的包含它的作用域。要引用其他作用域中的this,請使用標簽限定符: ### 限定的this 要訪問來自外部作用域的this(一個[類](http://www.kotlincn.net/docs/reference/classes.html)或者[擴展函數](http://www.kotlincn.net/docs/reference/extensions.html), 或者帶標簽的[帶有接收者的函數字面值](http://www.kotlincn.net/docs/reference/lambdas.html#帶有接收者的函數字面值))我們使用`this@label`,其中`@label`是一個代指this來源的標簽: ``` class A { // 隱式標簽 @A inner class B { // 隱式標簽 @B fun Int.foo() { // 隱式標簽 @foo val a = this@A // A 的 this val b = this@B // B 的 this val c = this // foo() 的接收者,一個 Int val c1 = this@foo // foo() 的接收者,一個 Int val funLit = lambda@ fun String.() { val d = this // funLit 的接收者 } val funLit2 = { s: String -> // foo() 的接收者,因為它包含的 lambda 表達式 // 沒有任何接收者 val d1 = this } } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看