----《第一季Kotlin崛起:次世代Android开发 》学习笔记
总目录:每天学一点 Kotlin ---- 目录
上一篇:每天学一点 Kotlin -- 对象进阶:对象比较
下一篇:每天学一点 Kotlin -- 对象进阶:类型别名
1. this 表达式
- this 指代当前类的实例,但是如果代码里面的类很复杂,可能嵌套了各种的方法和扩展函数或者内部类,那么如何让this指代特定的成员呢?
1.2 举个栗子
1.2.1 场景1:
class ThisPerson(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent(id: String) {
val studentId = id
val studentInfo: String
get() = "id = ${this.studentId}, name = ${name}, age = ${age} "
}
}
fun testThis() {
val p = ThisPerson("person", 30)
val s = p.ThisStudent("id-01")
p.printPersonInfo()
println(s.studentInfo)
}
fun main() {
testThis()
}
打印结果:
ThisPerson -- printPersonInfo() -- name = person, age = 30
id = id-01, name = person, age = 30
在上面的代码中,外部类的 printPersonInfo() 方法中,this 指的是外部类 ThisPerson;在内部类的 getter() 方法中,this 指的是内部类 ThisStudent。因此可以得出:this 指的是最小范围内的当前对象。
1.2.2 场景2:
将上面代码的基础上,如果外部类和内部类都有相同名称的属性呢?
class ThisPerson1(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson1 -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent1(id: String, name: String, age: Int) {
val studentId = id
val name = name
val age = age
val studentInfo: String
get() = "id = ${this.studentId}, name = ${name}, age = ${age} "
}
}
fun testThis1() {
val p1 = ThisPerson1("person", 30)
val s1 = p1.ThisStudent1("id-01", "student", 18)
p1.printPersonInfo()
println(s1.studentInfo)
}
fun main() {
testThis1()
}
打印结果:
ThisPerson1 -- printPersonInfo() -- name = person, age = 30
id = id-01, name = student, age = 18
从打印结果得到,有相同名称的属性时,内部类使用的是自己的属性
1.2.3 场景3:
在场景2代码的基础上再次修改:在使用相同名称的属性,通过 this 来调用
class ThisPerson2(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson2 -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent2(id: String, name: String, age: Int) {
val studentId = id
val name = name
val age = age
val studentInfo: String
get() = "id = ${this.studentId}, name = ${this.name}, age = ${this.age} "
}
}
fun testThis2() {
val p2 = ThisPerson2("person", 30)
val s2 = p2.ThisStudent2("id-02", "student", 18)
p2.printPersonInfo()
println(s2.studentInfo)
}
fun main() {
testThis2()
}
打印结果:
ThisPerson2 -- printPersonInfo() -- name = person, age = 30
id = id-02, name = student, age = 18
从打印结果看,和场景2中是相同的,从而也验证了场景1中的结论。
1.3 在内部类中访问外部类中独有的属性:如果某些属性只在外部类中有,那么内部类直接访问外部类的这些属性就可以。内部类没有的属性,在访问时是不可以在之前加上 this 的,因为这些属性只有外部类有么。举个栗子,将场景1中的代码改为下面这样写时,编译器会直接报错的。
val studentInfo: String
get() = "id = ${this.studentId}, name = ${this.name}, age = ${this.age} "
1.4 当外部类和内部类的属性名称相同时,使用 “this@外部类名称” 再加点号加属性名称,则访问的是外部类的属性。举个栗子:
class ThisPerson3(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson3 -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent3(id: String, name: String, age: Int) {
val studentId = id
val name = name
val age = age
val studentInfo: String
get() = "id = ${this.studentId}, name = ${this@ThisPerson3.name}, age = ${this@ThisPerson3.age} "
}
}
fun testThis3() {
val p3 = ThisPerson3("person", 30)
val s3 = p3.ThisStudent3("id-02", "student", 18)
p3.printPersonInfo()
println(s3.studentInfo)
}
fun main() {
testThis3()
}
打印结果:
ThisPerson3 -- printPersonInfo() -- name = person, age = 30
id = id-02, name = person, age = 30
从打印结果看,对于相同名称的属性,在内部类访问的是外部类中的属性。
举一反三:使用 “this@内部类名称” 再加点号加属性名称,则访问的是内部类的属性。
3. 在扩展函数中使用 this
3.1 在内部类定义扩展函数的情况下,this 指的是什么呢?举个栗子:
class ThisPerson4(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson4 -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent4(id: String, name: String, p: ThisPerson4) {
val studentId = id
val name = name
val person = p
val studentInfo: String
get() = "id = ${this.studentId}, name = ${name}, age = ${age} "
fun ThisPerson4.sayHello() = println("hello, I'm ThisPerson4, name = ${this.name}")
fun letPersonSayHello() {
person.sayHello()
}
}
}
fun testThis4() {
val p4 = ThisPerson4("person", 30)
val s4 = p4.ThisStudent4("id-02", "student", p4)
p4.printPersonInfo()
println(s4.studentInfo)
s4.letPersonSayHello()
}
fun main() {
testThis4()
}
打印结果:
ThisPerson4 -- printPersonInfo() -- name = person, age = 30
id = id-02, name = student, age = 30
hello, I'm ThisPerson4, name = person
在扩展函数中,this 表示扩展函数的点左侧传递的接收者参数。
4. 在 Lambda 表达式中使用 this
4.1 在 Lambda 表达式中 this 指代的是定义函数类型属性的类,也就是含有这个 Lambda 表达式的对象。
4.2 举个栗子:
class ThisPerson5(name: String, age: Int) {
val name: String = name
val age: Int = age
fun printPersonInfo() {
println("ThisPerson5 -- printPersonInfo() -- name = ${this.name}, age = ${this.age}")
}
inner class ThisStudent5(id: String, name: String) {
val studentId = id
val name = name
val studentInfo: String
get() = "id = ${this.studentId}, name = ${name}, age = ${age} "
val nameAddingStr: (String) -> String = { str -> this.name + str }
}
}
fun testThis5() {
val p5 = ThisPerson5("person", 30)
val s5 = p5.ThisStudent5("id-02", "student")
p5.printPersonInfo()
println(s5.studentInfo)
println(s5.nameAddingStr("-s5"))
}
fun main() {
testThis5()
}
打印结果:
ThisPerson5 -- printPersonInfo() -- name = person, age = 30
id = id-02, name = student, age = 30
student-s5
网友评论