阅读背景:

Scala 伴生类&伴生对象 apply

来源:互联网 
object Test {
  def main(args: Array[String]): Unit = {
    //Object Student就是一个单例
    println(Student.score)
    //Student() 不是new出来的,会去调用apply方法
    val student = Student()
    student.study()
  }
}

class Student{

  def apply():Unit = {
    println("class Student apply")
  }

  def study():Unit = {
    println("study hard")
  }
}

object Student{
  def score:Int = 100

  def apply():Student = {
    println("[enter] object student apply")
    new Student
  }
}object Test {
  def main(args: Array[String]



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: