scala入门系列(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35744460/article/details/89705548
/**
  * 构造函数
  */
object ConstructDemo {


  def main(args: Array[String]): Unit = {
    val pepole = new Pepole("a",23,"男")
    println( pepole.name+" : "+pepole.age +" : " +pepole.sex)
  }
}
//主构造器(跟在class后面的)
class Pepole(val  name:String,val age:Int){
  println("pepole开始")
  var sex:String=_
  //附属构造器
  //第一行必须是主构造器或其他附属构造器
  def this( name:String, age:Int,sex:String){
    this(name,age) //***********
    println("附属构造器")
    this.sex=sex
  }
  println("pepole结束")
}

猜你喜欢

转载自blog.csdn.net/qq_35744460/article/details/89705548
今日推荐