Created
June 17, 2021 06:04
-
-
Save CheolhoJeon/3d65032a2d06ec8f9181405c2f321a65 to your computer and use it in GitHub Desktop.
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
package chap5.CompanionObjects | |
import atomictest.trace | |
interface ZI { | |
fun f(): String | |
fun g(): String | |
} | |
open class ZIOpen : ZI { | |
override fun f() = "ZIOpen.f()" | |
override fun g() = "ZIOpen.g()" | |
} | |
class ZICompanion { | |
companion object: ZIOpen() | |
fun u() = trace("${f()} ${g()}") | |
} | |
class ZICompanionInheritance { | |
companion object: ZIOpen() { | |
override fun g() = | |
"ZICompanionInheritance.g()" | |
fun h() = "ZICompanionInheritance.h()" | |
} | |
fun u() = trace("${f()} ${g()} ${h()}") | |
} | |
class ZIClass { | |
companion object: ZI { | |
override fun f() = "ZIClass.f()" | |
override fun g() = "ZIClass.g()" | |
} | |
fun u() = trace("${f()} ${g()}") | |
} | |
fun main() { | |
ZIClass.f() | |
ZIClass.g() | |
ZIClass().u() | |
ZICompanion.f() | |
ZICompanion.g() | |
ZICompanion().u() | |
ZICompanionInheritance.f() | |
ZICompanionInheritance.g() | |
ZICompanionInheritance().u() | |
trace eq """ | |
ZIClass.f() ZIClass.g() | |
ZIOpen.f() ZIOpen.g() | |
ZIOpen.f() | |
ZICompanionInheritance.g() | |
ZICompanionInheritance.h() | |
""" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment