Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created June 17, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CheolhoJeon/3d65032a2d06ec8f9181405c2f321a65 to your computer and use it in GitHub Desktop.
Save CheolhoJeon/3d65032a2d06ec8f9181405c2f321a65 to your computer and use it in GitHub Desktop.
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