Using the constructor call parent scala subclass

ccn.aura.test.demo01.Dtmo.DD Package Penalty for
// Note scala only the primary constructor can directly call the constructor of the parent class (main and auxiliary constructor constructor) enough to create a subclass of aid can not directly call the parent class the discharge enough to create an indirect method must call the parent class to be created by the primary subclass enough to create an
Object CaoLei {
DEF main (args: the Array [String]): unit = {
// call the parent class be no arguments made
// new new var = zi and Zi
// zi.name = "start"
// = 22 is zi.age
// zi.show ()
// call the parent class has enough to create reference
var = ZI1 new new Zi1
zi1.name = " start "
zi1.age = 22 is
zi1.show ()
}
}
class {Fu
the println (" this is enough to create the parent master device ")
var name: String = _
var Age: Int = _
DEF the this (name: String, Age : Int) {
the this ()
this.name name =
this.age = Age
the println ( "parent auxiliary constructor")
}

}
// call is not enough to create a reference to the parent
class Zi (Gender: Int) the extends Fu {
the println ( "primary constructor subclass")
DEF the this () {
the this (. 1)
the println (secondary structure "subclass device ")
}
DEF Show () {
the println (S" name: n a m e , a g e : {name},age: age,gender:$gender")

}}
// call the parent class has enough reference is made
class Zi1 (name: String, Age: Int, Gender: Int) the extends Fu (name: String, Age: Int) {
the println ( "primary constructor subclass" )
DEF the this () {
the this ( "wangmign", 10,1)
the println ( "auxiliary subclass constructor")
}
DEF Show () {
the println (S "name: n a m e , a g e : {name},age: age,gender:$gender")

}}

Guess you like

Origin blog.csdn.net/weixin_44701192/article/details/91946755