Created
June 16, 2021 03:57
-
-
Save CheolhoJeon/84f7468105ff0dcbd0808326f60a04bb 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.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