#odersky-programming-in-scala-1ed #scala
When a singleton
object shares the same name with a class, it is called that class's
companion object.
You must define both
the class and its companion object in the same source file. The class
is called the
companion class of the singleton object. A class
and its companion object can access each other's private members.
If you want to change selection, open document below and click on "Move attachment"
Classes and Objectss like a class definition,
except instead of the keyword class you use the keyword
object. Listing 4.2 shows an example.
The singleton object in this figure is named ChecksumAccumulator,
the same name as the class in the previous example. <span>When a singleton
object shares the same name with a class, it is called that class's
companion object. You must define both
the class and its companion object in the same source file. The class
is called the companion class of the singleton object. A class
and its companion object can access each other's private members.
The ChecksumAccumulator singleton object has one method,
named calculate, which takes a String and calculates a checksum for
the characters in the String. It also has one private field, Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details