辅助构造器

class Person {
  /*
  1.辅助构造器的名称为this.
  2.每一个辅助构造器的第一行必须调用其他辅助构造器或者主构造器.
   */

  //如果一个类没有显式定义主构造器则自动拥有一个无参的主构造器
  private var name = ""
  private var age = 0

  def this(name: String){
    this()  //调用主构造器
    this.name = name
  }

  def this(name: String, age: Int){
    this(name)  //调用其他的辅助构造器
    this.age = age
  }
}
posted @ 2017-05-23 12:28  dongdone  阅读(555)  评论(0编辑  收藏  举报