Scala Classic: 29,case class and case object code combat analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.