Scala sample class

Article Directory


1 Overview

In Scala, the sample class is a special class, generally used to save data (similar to Java POJO class), it is often used in concurrent programming and frameworks such as Spark and Flink.

2. Format

case class 样例类名([var/val] 成员变量名1:类型1, 成员变量名2:类型2, 成员变量名3:类型3){
    
    }
  • Variable modifier can not be written, if you do not write, the default modifier is val
  • If you want to realize that the value of a member variable can be modified, you need to manually add var to modify this variable

3. Case

demand:

  • Define the sample class Person, which contains two member variables: name and age.

    其中: 姓名用val修饰, 年龄用var修饰

  • Create an object of the Person class in the test class and print its attribute values.

  • Try to modify the values ​​of the two member variables, name and age, and observe the results.

Code:

object demo {
    
    
  //1. 创建一个Person样例类, 属性为: 姓名, 年龄.
  case class Person(name:String = "张三",var age:Int = 23) {
    
    }

  def main(args: Array[String]): Unit = {
    
    
    //2. 创建Person类型的对象, 然后打印属性值.
    val p = new Person()
    println(p)
    //3. 尝试修改对象p的属性值
    //p.name = "李四"     //这样写会报错, 因为样例类的成员变量默认修饰符是: val
    p.age = 24
    println(p)
  }
}

4. The default method in the sample class

When we define a sample class, the compiler will automatically help us generate some methods, the commonly used ones are as follows:

  • apply() method
  • toString() method
  • equals() method
  • hashCode() method
  • copy() method
  • unapply() method

Detailed function:

  • apply() method

    • Allows us to quickly use the class name to create objects, eliminating the new keyword
    • E.g: val p = Person()
  • toString() method

    • It allows us to print the value of each attribute of the object directly when printing the object through the output statement.
    • E.g: println(p) 打印的是对象p的各个属性值, 而不是它的地址值
  • equals() method

    • It can be used directly ==to compare whether the values ​​of all the member variables of the two sample class objects are equal.
    • E.g: p1 == p2 比较的是两个对象的各个属性值是否相等, 而不是比较地址值
  • hashCode() method

    • Used to obtain the hash value of an object. That is: the hash value of the same object must be the same, and the hash value of different objects is generally different.
  • E.g:

    val p1 = new Person("张三", 23)
    val p2 = new Person("张三", 23)
    println(p1.hashCode() == p2.hashCode())		//结果为: true
    
  • copy() method

  • It can be used to quickly create an instance object with the same attribute value, and it can also assign a value to a specified member variable in the form of a named parameter.

  • E.g:

    val p1 = new Person("张三", 23)
    val p2 = p1.copy(age = 24)
    println(p1)		//结果为: 张三, 23
    println(p2)		//结果为: 张三, 24
    
  • unapply() method

    • Generally used 提取器, but it requires some foreshadowing knowledge, we haven't learned it yet, it will be explained in detail in the subsequent chapters.

Guess you like

Origin blog.csdn.net/zh2475855601/article/details/114766136