Scala study notes -00- associated objects, isolated objects

Scala uses singleton object (singleton object, i.e. object) to achieve the same java static member functions, i.e. object. Methods / properties

Associated Objects

Singleton object there is a special case: companion objects, companion object.

When a single object and its namesake interest Appearing together, the simple interest object is referred to as "companion object" class of the same name, the same, the class is called the simple interest companion class object.

Class and associated objects must exist in the same file, you can access private members each other.

Methods such as scala.io.Source have a companion class objects, class Source does not fromFile ( "***") is, object Source has fromFile method ( "***") of

https://www.scala-lang.org/api/2.11.12/#scala.io.Source: 

https://www.scala-lang.org/api/2.11.12/#scala.io.Source$

 

Orphaned Objects

Singleton object class is not the same name, called "orphan object" (standalone object).

In general, the main entry point of the program The method defined in scala an isolated object.

 

Guess you like

Origin www.cnblogs.com/wooluwalker/p/12297717.html