Scala Constructors and Auxiliary Constructors
原创转载请注明出处:http://agilestyle.iteye.com/blog/2333245
Constructors
The constructor is the code that "constructs" a new object. The constructor is the combined effect of the class argument list - initialized before entering the class body - and the class body, whose statements execute from top to bottom.
The simplest form of a constructor is a single line class definition, with no class arguments and no executable lines of code, such as:
class Person
In Fields, the constructor initializes the fields to the values specified, or to defaults if no values were specified. In Class Arguments, the constructor quietly initializes the arguments and makes them accessible to other objects; it also unravels a variable argument list.
In those cases, we didn’t write constructor code – Scala did it for us. For more customization, add your own constructor code. For example:
package org.fool.scala.constructors class Coffee(val shots: Int = 2, val decaf: Boolean = false, val milk: Boolean = false, val toGo: Boolean = false, val syrup: String = "") { var result = "" println(shots, decaf, milk, toGo, syrup) def getCup(): Unit = { if (toGo) result += "ToGoCup " else result += "HereCup " } def pourShots(): Unit = { for (s <- 0 until shots) if (decaf) result += "decaf shot " else result += "shot " } def addMilk(): Unit = { if (milk) result += "milk " } def addSyrup(): Unit = { result += syrup } getCup() pourShots() addMilk() addSyrup() } object ConstructorsTest extends App { val usual = new Coffee() println(usual.result) val mocha = new Coffee(decaf = true, toGo = true, syrup = "Chocolate") println(mocha.result) }
Console Output
Auxiliary Constructors
Named and default arguments in the class argument list can construct objects in multiple ways. We can also use constructor overloading by creating multiple constructors. The name is overloaded here because you’re making different ways to create objects of the same class. To create an overloaded constructor you define a method (with a distinct argument list) called this (a keyword). Overloaded constructors have a special name in Scala: auxiliary constructors.
Because constructors are responsible for the important act of initialization, constructor overloading has an additional constraint: all auxiliary constructors must first call the primary constructor. This is the constructor produced by the class argument list together with the class body. To call the primary constructor within an auxiliary constructor, you don’t use the class name, but instead the this keyword:
package org.fool.scala.constructors class GardenGnome(val height: Double, val weight: Double, val happy: Boolean) { println("Inside primary constructor...") var painted = true def magic(level: Int): String = "Poof! " + level def this(height: Double) { this(height, 100.0, true) } def this(name: String) = { this(15.0) painted = false } def show(): String = height + " " + weight + " " + happy + " " + painted } object AuxiliaryConstructorsTest extends App { val g1 = new GardenGnome(20.0, 110.0, false) println(g1.show()) println(g1.magic(1)) println(new GardenGnome("MyGarden").show()) println(new GardenGnome("MyGarden").magic(2)) }
Console Output
Reference
Atomic Scala 2nd Edition - Construtors