this 表达式


含义

  1. class 中代表该类的对象
  2. 扩展function中是点号左边接收器
fun BaseMain() {
    var stu = Student("Nibin")
    stu.gogo()
    stu.funt()
}

class Student(name: String) {
    var name: String = ""

    init {
        this.name = name
    }

    fun gogo() {
        println("Let us go go ,${this.name}")
    }
}

fun Student.funt() {
    println("This is your name of ${this.name}")
}
/**
Let us go go ,Nibin
This is your name of Nibin
*/

标签@lable

当this 不知道指定哪一个对象时,可以利用@lable 定位指定的this,如下

fun BaseMain() {
    var stu = Student("Nibin")
    println(stu.Book().content())
}

class Student(name: String) {
    var name: String = ""

    init {
        this.name = name
    }

    inner class Book {
        var name = "Harry Potter"
        fun content(): String {

            return "Hellow word: book name is ${[email protected]}  and the student name is  ${[email protected]}"
        }
    }

}

/**
Hellow word: book name is Harry Potter  and the student name is  Nibin
*/

results matching ""

    No results matching ""