/*class ConstructorDemo {
var a: Int = 0
println(" Constructor study")
//定义辅助构造函数
def this(a1: Int) {
//需要调用主构造函数或者其他辅助构造函数
this()
this.a = a1
}
}*/
//定义带有参数的主构造函数
/*class ConstructorDemo(var b: Int) {
var a: Int = 0
println(" Constructor study")
}*/
/*class ConstructorDemo(b: Int) {
var a: Int = 0
println(" Constructor study")
//定义辅助构造函数
def this(a1: Int, b1: Int) {
this(b1)
this.a = a1
}
}*/
//定义私有的主构造函数
class ConstructorDemo private(b: Int) {
var a: Int = 0
println(" Constructor study")
//定义辅助构造函数
def this(a1: Int, b1: Int) {
this(b1)
this.a = a1
}
}
//类自带有无参构造函数
//主构造函数会执行类中定义的所有语句
//构造函数分为主构造函数和辅助构造函数
object ConstructorDemoTest {
def main(args: Array[String]): Unit = {
/* val demo = new ConstructorDemo()
val demo2 = new ConstructorDemo(2)
println(demo.a)
println(demo2.a)*/
}
}
07-09
459
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交