Member variables

In Java, we define a class like this:

public class JavaPerson { private String name; private Integer age; // Omit the setter and getter methods for name, age, and age;

The call looks like this:

public static void main(String[] args) { JavaPerson javaPerson = new JavaPerson(); javaPerson.setAge(18); JavaPerson. Elegantly-named setName (" zhang "); Println (" Name: "+ javaperson.getName () +", age: "+ javaperson.getAge ()); }

In Scala, it’s a little bit more concise. The class definition is as follows. Since all variables in Scala are defined with an initial value, we can define the underscore here as blank.

class ScalaPerson {
  var age: Integer = _
  var name: String = _
}

The call looks like this:

Val scalaPerson = new scalaPerson scalaPerson. Age = 18 scalaPerson. Name = "" println(s) ${scalaperson.name}, age :${scalaperson.age}")

It looks like we’re calling the properties of the object directly, but Scala is actually hiding the setter and getter, and if we look at the decompilated code, this is the decompilated code of Scalaperson, you can see that its properties are actually private, and then there are methods like setters and getters, The age() and age_$eq() methods are examples



Sometimes, for compatibility with the Java framework, we need setters and getters, and we just add them to the properties@BeanPropertyAnnotations can use setters and getters.

class ScalaPerson2 { @BeanProperty var age: Integer = _ var name: String = _ } object ScalaPerson2 { def main(args: Array[String]): Unit = {val Scalaperson = new Scalaperson 2 Scalaperson. age = 17 Scalaperson. setAge(18) Scalaperson. name =" Println (s" Name :${scalaperson.name}, Age :${scalaperson.getage}")}

We know that Scala’s variable declarations include val and var. If val is defined ina class, the variable is of final type. For example, the name above is defined by val, which is similar to the Java code below, and only has a get method.

Private final String name = "String name ";

The constructor

No arguments structure

If there are no arguments, the class name may or may not be enclosed in parentheses

class ScalaConstruct1 { var name: String = _ var age: Int = _ } object ScalaConstruct1 { def main(args: Array[String]): Unit = {val Construct = new ScalaconStruct1 Construct. Name = ScalaconStruct1 Construct. Age = 18}

There is a parameter constructor

Variable modifiers with arguments include no modifiers, val, and var. The following examples illustrate these three situations.

class ScalaConstruct2(param1: String, var param2: String, val param3: String) {

}

object ScalaConstruct2 {
  def main(args: Array[String]): Unit = {
    val construct = new ScalaConstruct2("aa", "bb", "cc")
    //println(construct.param1) //error
    //construct.param1 = "aaa" // error
    construct.param2 = "bbb"
    println(construct.param2)
    //construct.param3 = "ccc"// error
    println(construct.param3)
  }
}

Param1 has no modifier, so it is a local variable to ScalaconStruct2, which cannot be called outside of the class. Param2 is modifiable, so it can be read and modified. Param3 cannot be modified, so it can only be read but not modified.

The main constructor

The primary constructor is the one that has no arguments above and the one that has arguments after the class name. In addition, the primary constructor can execute any statement that can be executed in the class. So what I’m going to do is I’m going to print Hello ScalaconStruct3

class ScalaConstruct3() {
  println("hello ScalaConstruct3")
}

object ScalaConstruct3{
  def main(args: Array[String]): Unit = {
    new ScalaConstruct3
  }
}

Auxiliary constructor

Java uses the class name as the constructor name. In Scala, the auxiliary constructor is declared with this. It must directly or indirectly call the primary constructor. Class, similar to Java.

class ScalaConstruct4() { var age: Int = _ var name: String = _ def this(name: String) { this() this.name = name } def this(name: String, age: Int) { this(name) this.age = age } } object ScalaConstruct4 { def main(args: Array[String]): Unit = {new ScalaconStruct4 new ScalaconStruct4 (") "new ScalaconStruct4 (") ",18)}