[Scala] Constructor

I was wandering around in the official reference about the Augji Realist constructor (this (), or .this), but instead of not finding it, there was an article about the constructor as secondary information, so the content is roughly in the literature. It is a translation.

The constructor is used to initialize the state of the object. Like methods, a constructor also contains a set of instructions that are executed when an object is created.

Scala supports two types of constructors.

  1. Primary constructor
  2. Augji Realist Constructor

When a program contains only one constructor, it is called the primary constructor. Since the primary constructor and the class share the same body, the programmer does not need to explicitly declare the creation of the constructor.

class Hello(name) {
  // Statements
}

--In the above syntax, the primary constructor shares the same body as the class. Except for method declarations, everything defined in the body of a class is part of the primary constructor.

//Create primary constructor
class Monster(name: String, attribute: String, attack: Int, defense: Int) {
  def status: String = {
    s"""
       |Name: $name
       |Attribute: $attribute
       |Attack: $attack
       |Defense: $defense
       |""".stripMargin
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    //Object creation and initialization
    val dmg = new Monster("Dark Magician Girl", "Dark", 2000, 1700)
    println(dmg.status)
  }
}

Name: Dark Magician Girl
Attribute: Dark
Attack: 2000
Defense: 1700

--The primary constructor has zero or more parameters. --If you do not create a constructor in your program, the compiler will automatically create a primary constructor when you create an object from a class. This constructor is called the default primary constructor and does not receive any parameters.

//Create primary constructor
class Nap {
  def zzz: String = {
    "Zzz.."
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    //Object creation and initialization
    val nobita = new Nap
    println(nobita.nap)
  }
}
Zzz..

--Declaring the constructor parameters with var allows you to change the value of the field and generate getter and setter methods for the field. --If you declare a constructor parameter with val, you cannot change the value of the field. Only getter methods are generated in the field. --If you declare the constructor parameters without var or val, the visibility of the fields will be very limited. No getter or setter methods are generated in the field. --Declaring constructor parameters with private var or private val prevents the field getter and setter methods from being generated. Only members of that class can access the field. --Only the primary constructor can invoke the superclass constructor. --You can make the primary constructor private by using the private keyword between the class name and the parameter.

//With parameters
class Secret private (n: Int) {
  // Statements
}

//No parameters
class Privacy private {
  // Statements
}

--You can set default values for parameters when declaring a constructor.

class User(val name: String = "Anonymous") {
  def show: String = s"Name: $name"
}

object Main {
  def main(args: Array[String]) = {
    val usr = new User
    println(usr.show)
  }
}

Constructors other than the primary constructor are called Augji Real Constructors, and you can create as many as you like. However, the program has only one primary constructor.

class Apple(product: String, model: String) {
  var color: String = ""

  def details: String = {
    s"""
      |Product name: $product
      |Model: $model
      |Color: $color
      |""".stripMargin
  }

  //Augji Realist Constructor
  def this(name: String, model: String, color: String) = {
    //Start primary constructor
    this(name: String, model: String)
    this.color = color
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val device: Apple = new Apple("iPhone", "iPhone 11 Pro", "Gold")
    println(device.details)
  }
}
Product name: iPhone
Model: iPhone 11 Pro
Color: Gold

Literature

Scala Constructors - GeeksforGeeks

Recommended Posts

[Scala] Constructor
constructor
java (constructor)
Java constructor
Understand java constructor
I touched Scala