Scala class case study notes -08- sample class

When defining a class, plus the case if the keyword before the keyword class, then the class known as case classes.

is a special case classes classes that are optimized for pattern matches

Scala case classes for the automatic reload many useful methods, including toString, equals and hashcode methods.

Scala class for each case automatically generating a companion objects , comprising template code

  • An apply method, therefore, when the class is instantiated case without using a new keyword ;
  • A unapply method comprising a class type associated parameter, the result is returned Option type, the type parameters of the corresponding N-tuple, N is the number of main companion class constructor arguments. A method for object Unapply deconstruction operation
    • In the case class pattern matching , the process is automatically invoked, and the object to be matched as a parameter passed to it

 

// consider the definition of a class Case: 
Scala> Case class Car (Brand: String,. Price: Int) defined class Car // compiler automatically generates the associated objects: Scala> Object Car { | DEF Apply (Brand: String,. Price: int) = new new Car (Brand,. price) | DEF unapply (C: Car): Option-[(String, int)] = s Some ((c.brand, c.price)) |} defined Object Car

 

Guess you like

Origin www.cnblogs.com/wooluwalker/p/12306030.html