oop - Scala, how do I replace static members? -
i wan't build class animal can find number of animals created. in scala there no option static variable, how can implement such functionality in scala (i looking non-specific solution)?
thanks!
for example in java:
public class amimal { static int number_of_aminals = 0; public animal() { number_of_animals++; } }
one option be:
import animal class animal { animal.increment() } object animal { private[this] var _count = 0 def increment(): unit = { _count += 1 } def count: int = _count }
though might want use atomicint.
Comments
Post a Comment