Scala entry notes - associated objects

Associated objects: the class name and the same modified with Object object, the object class and associated methods and can access each other private properties

package day03

class Dog {
  private var name = "dog"

  def printName() : Unit = {
    println(Dog.CONSTANT + name)
  }
}

object Dog {
  private val CONSTANT = "wolf, wolf, wolf"

  def main(args: Array[String]): Unit = {
    val p = new Dog

    // 访问类中私有字段
    println(p.name)

    p.name = "big huang"

    p.printName()
  }
}

  

Guess you like

Origin www.cnblogs.com/sunnystone85/p/11361627.html