Scala Case Class
Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://yjplxq.blog.51cto.com/4081353/1415533
What does case classes do again? You can just think of him as a normal class, but slightly different, summed up as follows:
No need to write new, but can write
The default is public, which is called from anywhere
The ToString is implemented by default
Cannot be inherited
Official text:
It makes define case classes if pattern matching are used to decompose data structures.
Of course, it's a bit biased to have meaning in the pattern matching, at least some old programmers have other ideas:
Get auto-generated equals, Hashcode, toString, Static apply () for shorter initialization, etc.
such as the Xia Guan Network example,
/** * @author: lenovo (2015-04-13 13:37) */// Abstract Parent class abstract class term// case classcase class var (name: string) extends Term// case Classcase class fun (arg: string, body: term) extends Term// case Classcase class app0 (f: term, v: term) extends termobject termtest extends app { def printterm (term: term) { Term match { case var (n) => print (n) case fun (x, b) => print ("^" + x + ".") printterm (b) case app0 (F, V) => print ("(") printterm (f) print (" ") printterm (v) print (")") } } Def isidentityfun (term: term): boolean = term match { case fun (X, var (y)) if x == y => true Case _ => false } val id = fun ("X", Var ("X")) val t = fun ("X", fun ("Y", app0 (Var ("X"), var ("Y"))) Printterm (t) println println (Isidentityfun (ID)) println (IsIdentityFun (t))}
Run
^x.^y. (x y) TrueFalse
Scala Case Class