Scala--构造器
1、scala的主构造器和辅助构造器,scale可以有多个辅助的构造器,但是只能有一个主构造器
package com.itstar.scala.test/** * 定义学生类并且使用了主构造器 * Scala里面只有一个主构造器,可以有多个辅助构造器 * @param stuName * @param stuAge */class Student3(var stuName:String,var stuAge:Int) { //定义一个属性 private var gendel:Int = 0 //定义一个参数的辅助构造器 def this(age:Int) = { this("Mike",age) println("this(age:Int)") } //定义没有参数的辅助构造器 def this()={ this(20) println("this()") }}object Student3{ def main(args: Array[String]): Unit = { //先使用主构造器 println("===========使用主构造器============") var s = new Student3("Tom",20) println(s.stuName + "\t" + s.stuAge + "\t" + s.gendel) println("===========使用带一个参数的辅助构造器============") var s1 = new Student3(30) s1.stuName = "Nick" s1.gendel = 1 println(s1.stuName + "\t" + s1.stuAge + "\t" + s1.gendel) println("=================使用不带参数的辅助构造器========================") var s3 = new Student3() println(s3.stuName + "\t" + s3.stuAge + "\t" + s3.gendel) }}
相关推荐
匆匆那些年 2020-10-15
TheBigBlue 2020-07-28
shenwenjie 2020-07-07
muhongdi 2020-07-07
waitwolf 2020-07-08
yunfenglee 2020-07-08
yunfenglee 2020-07-08
kekeromer 2020-07-08
匆匆那些年 2020-07-07
liqinglin0 2020-07-05
TheBigBlue 2020-07-05
kekeromer 2020-06-13
zhixingheyitian 2020-06-08
TheBigBlue 2020-06-06
liqinglin0 2020-06-01
liqinglin0 2020-06-01
yunfenglee 2020-05-30
MICKEYSTUDY 2020-05-28
muhongdi 2020-05-19