Explanation of singleton objects in Scala

后端 未结 4 533
Happy的楠姐
Happy的楠姐 2021-01-31 03:16

I get the coding in that you basically provide an \"object SomeClass\" and a \"class SomeClass\" and the companion class is the class declaration and the object is a singleton.

4条回答
  •  天命终不由人
    2021-01-31 03:24

    In many cases we need a singleton to stand for unique object in our software system. Think about the the solar system. We may have following classes

    class Planet
    object Earth extends Planet
    object Sun extends Planet
    

    object is a simple way to create singleton, of course it is usually used to create class level method, as static method in java

提交回复
热议问题