Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhu.tao
committed
Aug 4, 2020
1 parent
841b313
commit bd91d3d
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/boycoder/kotlinjetpackinaction/chapter/c06/TestExtAsMember.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.boycoder.kotlinjetpackinaction.chapter.c06 | ||
|
||
/** | ||
* Created by zhu.tao on 2020/7/31. | ||
*/ | ||
|
||
open class Base { } | ||
|
||
class Derived : Base() { } | ||
|
||
open class BaseCaller { | ||
open fun Base.printFunctionInfo() { | ||
println("Base extension function in BaseCaller") | ||
} | ||
|
||
open fun Derived.printFunctionInfo() { | ||
println("Derived extension function in BaseCaller") | ||
} | ||
|
||
val Derived.test: Int | ||
get() = 1 | ||
|
||
fun call(b: Base) { | ||
b.printFunctionInfo() // 调用扩展函数 | ||
} | ||
} | ||
|
||
class DerivedCaller: BaseCaller() { | ||
override fun Base.printFunctionInfo() { | ||
println("Base extension function in DerivedCaller") | ||
} | ||
|
||
override fun Derived.printFunctionInfo() { | ||
println("Derived extension function in DerivedCaller") | ||
} | ||
} | ||
|
||
/** | ||
* 步骤:先运行代码,然后调试代码,最后反编译代码。 | ||
* | ||
* 理解这个例子的关键在于: | ||
* | ||
* BaseCaller().call(), DerivedCaller().call() 是多态的。 | ||
* | ||
* 而 call 函数里的 base.printFunctionInfo() 是静态的。 | ||
* | ||
* 这段话一定要结合反编译后的代码看 | ||
* | ||
*/ | ||
fun main() { | ||
BaseCaller().call(Base()) | ||
BaseCaller().call(Derived()) | ||
DerivedCaller().call(Base()) | ||
DerivedCaller().call(Derived()) | ||
} |