Today, I studied the 29th lecture of Liaoliang's Scala lecture, the application of case class and case object. Make a note of it.
Information from DT Big Data Dream Factory public account: Dt_spark
Follow the account to learn more about the Liaoliang Teacher's course content
Teacher Wang qq:1740415547
No.: 18610086859
Not much to say, let's start with the code:
Abstract class people//-defined abstraction classes
Case Class Student (age:int) extends people//defining case class, inheriting abstract class
Case Class Worker (age:int,salary:double) extends people
Case Object Shared extends people//defines case object, inherits abstract class
Object testcassclass{
def main (args:array[string]) {
def caseops (people:people) =people match{//define Caseops method for pattern matching of input parameter people
Case Student (age) =>println (' Age: ' + age)
Case Worker (age,salary) =>println ("Age:" +age + "salary" + salary)
Case Shared = println ("Nothing")
}
Caseops (Student (19))
Caseops (Shared)
Val worker = Worker (20,15000.0)
Val worker2 = worker.copy (salary=16000.0)//case class messaging capabilities
Val Worker3 = worker2.copy (age = 30)
}
}
There are two kinds of functions for case class and object objects, passing messages and pattern matching, and we often used the case class in pattern matching.
We can also copy a new instance from an existing instance using the case class message passing function, and can define different properties from the original instance while copying.
Scala Classic: 29,case class and case object code combat analysis