scalaベース12-単例オブジェクト/伴生オブジェクト

687 ワード

/*
 *        , object class     ,                    
 * class object    ,              ,  private 
 *         class     ,            ,      
 */
class University {
  val id = University.newStudentNo
  private var number = 0
  
  def aClass(number: Int) = {
    this.number += number
  }
}

/*
 * object ,    java     
 *             
 *         ,        
 */
object University {
  private var studentNo = 0
  
  def newStudentNo = {
    studentNo += 1
    studentNo
  }
}

object HelloOOP {
  def main(args: Array[String]) {
    println(University.newStudentNo)
    println(University.newStudentNo)
    
    val arr = Array(1, 2, 3) //  new,               
  }
}