About the main constructor in scala

In the early scala study, we generally used the Java style member definition method when adding variables, such as the following code.

class Student{
    
    
  var name : String = _
}

Scala will generate a default constructor and Scala-style SetXXX(public java.lang.String name()) and GetXXX(public name_(java.lang.String)). In fact, Scala provides a more simplified way of defining member variables and constructors, that is, interweaving member variables and constructors together, for example:

object demo03 {
    
    
  def main(args: Array[String]): Unit = {
    
    
  }
}
class Person(var name : String,var age : Int){
    
    
}

By decompiling this code, we get:

E:\WorkSpace\scalaFiles\target\classes\com\mo\chapter06\myconstructor>javap -private Person.class
Compiled from "demo03.scala"
public class com.mo.chapter06.myconstructor.Person {
    
    
  private java.lang.String name;
  private int age;
  public java.lang.String name();
  public void name_$eq(java.lang.String);
  public int age();
  public void age_$eq(int);
  public com.mo.chapter06.myconstructor.Person(java.lang.String, int);

You can see that class Person(var name: String, var age: Int) does three things:
1. Defines a class and implements the constructor Person(var name: String, var age: Int).
2. The name and age member variables are defined.
3. The Setxxx() and Getxxx() methods in scala style are generated respectively.

If you use java language to achieve the same function as class Person(var name: String, var age: Int), you need the following code.

public class demo04 {
    
    
    String name ;
    int age ;
    public void setName(String name){
    
    
        this.name = name;
    }
    public String getName(){
    
    
        return name;
    }
    public void setAge(int age){
    
    
        this.age = age;
    }
    public int getAge(){
    
    
        return age;
    }
    public demo04(String name,int age){
    
    
        this.name = name;
        this.age = age;
    }
}

It can be seen that scala only uses one line of code to achieve the function of multiple lines of code in java.
In the Java language, if you want to perform certain actions when creating an object, you need to add the corresponding code in the constructor, but in scala, you only need to put the code directly in the defined class, for example:

object demo03 {
    
    
  def main(args: Array[String]): Unit = {
    
    
    val person = new Person("tom",18)
    //this is a part of constructor~~~
  }
}
class Person(val name :String , val age : Int){
    
    
  println("this is a part of constructor~~~")
  //println语句作为主构造器的一部分,在创建对象时被执行。
  override def toString: String = name + " " + age
}

Note that in the above code, a println statement is placed in the Person class body, and the statement will be used as a part of the main constructor and executed when the object is created. It is equivalent to the following java code.

public class demo04 {
    
    
    public static void main(String[] args) {
    
    
        new demo04("tom" , 18);
    }
    String name ;
    int age ;
    public void setName(String name){
    
    
        this.name = name;
    }
    public String getName(){
    
    
        return name;
    }
    public void setAge(int age){
    
    
        this.age = age;
    }
    public int getAge(){
    
    
        return age;
    }
    public demo04(String name,int age){
    
    
        System.out.println("this is a part of constructor~~~");
        this.name = name;
        this.age = age;
    }
}

Note:
**

  1. The function of the Scala constructor is to complete the initialization of the new object, and the constructor has no return value.
  2. The declaration of the main constructor is placed directly after the class name.
  3. The main constructor will execute all the statements in the class definition (put the statements written in the class into the main constructor). Here you can experience the integration of Scala's functional programming and object-oriented programming, that is: the constructor is also a method ( Function), passing parameters and using methods are no different from the previous function part. **

Guess you like

Origin blog.csdn.net/weixin_44080445/article/details/108785651