Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created June 17, 2021 07:23
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/fa731eef120511326d6e225c4123956f to your computer and use it in GitHub Desktop.
Save CheolhoJeon/fa731eef120511326d6e225c4123956f to your computer and use it in GitHub Desktop.
package chap5.CompanionObjects
import atomictest.eq
class Numbered2
private constructor(private val id: Int) {
override fun toString(): String = "#$id"
companion object Factory {
fun create(size: Int) =
List(size) { Numbered2(it) }
}
}
fun main() {
Numbered2.create(0) eq "[]"
Numbered2.create(5) eq
"[#0, #1, #2, #3, #4]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment