Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created June 16, 2021 03:57
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/84f7468105ff0dcbd0808326f60a04bb to your computer and use it in GitHub Desktop.
Save CheolhoJeon/84f7468105ff0dcbd0808326f60a04bb to your computer and use it in GitHub Desktop.
package chap5.CompanionObjects
import atomictest.eq
class WithCompanion {
companion object {
val i = 3
fun f() = i * 3
}
fun g() = i + f()
}
fun WithCompanion.Companion.h() = f() * i
/**
* 비교를 위해 내가 별도로 작성한 코드
*/
class WithObject {
object Nested {
val i = 3
fun f() = i * 3
}
fun g() = Nested.i + Nested.f()
}
fun WithObject.Nested.h() = f() * i
fun main() {
val wc = WithCompanion()
wc.g() eq 12
WithCompanion.i eq 3
WithCompanion.f() eq 9
WithCompanion.h() eq 27
val wo = WithObject()
wo.g() eq 12
WithObject.Nested.i eq 3
WithObject.Nested.f() eq 9
WithObject.Nested.h() eq 27
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment