scala半生オブジェクトと半生クラス

1311 ワード


object:    
object               ,            。
object         ,            


object           
 SparkContext     object

                 
        apply  
          new Person()    new


class Person object Person    
object Person class Person     
scala> /////////////////////

scala> object Person{
     |   println("scala")
     |   var salary = 0.0
     |   def getSalary = salary
     | }
defined module Person
warning: previously defined class Person is not a companion to object Person.
Companions must be defined together; you may wish to use :paste mode for this.

scala> Person.getSalary  --object     
scala      --     ,       
res131: Double = 0.0

scala> Person.getSalary
res132: Double = 0.0


scala> /////////////////////


scala> class Person{
     |   println("scala in class...")
     |   var salary = 0.1
     |   def getSalary = salary
     | }
defined class Person
warning: previously defined object Person is not a companion to class Person.
Companions must be defined together; you may wish to use :paste mode for this.

scala> Person.getSalary  --    object   
res133: Double = 0.0

scala> val p = new Person   --class  new    
scala in class...
p: Person = Person@2059a675

scala> p.getSalary
res134: Double = 0.1