Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we’ll see how they are useful in pattern matching.

Defining a case class

A minimal case class requires the keywords case class, an identifier, and a parameter list (which may be empty):

case class Book(isbn: String)

val frankenstein = Book("978-0486282114")

Notice how the keyword new was not used to instantiate the Book case class. This is because case classes have an apply method by default which takes care of object construction.

When you create a case class with parameters, the parameters are public vals.

case class Message(sender: String, recipient: String, body: String)
val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") println(message1.sender) // prints guillaume@quebec.ca
message1.sender = "travis@washington.us" // this line does not compile

You can’t reassign message1.sender because it is a val (i.e. immutable). It is possible to use vars in case classes but this is discouraged.

Comparison

Case classes are compared by structure and not by reference:

case class Message(sender: String, recipient: String, body: String)

val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val messagesAreTheSame = message2 == message3 // true

Even though message2 and message3 refer to different objects, the value of each object is equal.

Copying

You can create a (shallow) copy of an instance of a case class simply by using the copy method. You can optionally change the constructor arguments.

case class Message(sender: String, recipient: String, body: String)
val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg")
val message5 = message4.copy(sender = message4.recipient, recipient = "claire@bourgogne.fr")
message5.sender // travis@washington.us
message5.recipient // claire@bourgogne.fr
message5.body // "Me zo o komz gant ma amezeg"

The recipient of message4 is used as the sender of message5 but the bodyof message4 was copied directly.

Scala: Case classes的更多相关文章

  1. Scala:case class

    Scala:case class 1.Scala中class.object.case class.case object区别 1.1 class 和 object 关系 1.2 case class ...

  2. Programming In Scala笔记-第十五章、Case Classes和模式匹配

    本章主要分析case classes和模式匹配(pattern matching). 一.简单例子 接下来首先以一个包含case classes和模式匹配的例子来展开本章内容. 下面的例子中将模拟实现 ...

  3. 学习Scala: 初学者应该了解的知识

    Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...

  4. Scala:使用Sublime开发Scala

    Scala:使用Sublime开发Scala 第一步:[Tools][Build System][New Build System] 第二步:在打开的新文件中输入: { //"cmd&quo ...

  5. scala2.10.x case classes cannot have more than 22 parameters

    问题 这个错误出现在case class参数超出22个的时候. case classes cannot have more than 22 parameters 在scala 2.11.x版本以下时c ...

  6. Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC

    Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC pom.xml添加依赖 Java:方式一(亲测实用) 方式二:Scala 方式三:Java PhoenixJDBCU ...

  7. 客户端,Scala:Spark查询Phoenix

    客户端,Scala:Spark查询Phoenix 1.pom.xml 2.配置文件 2.1config.properties 2.2MyConfig 3.entity实体(与phoenix中的tabl ...

  8. android switch语句报错:case expressions must be constant expressions

    今天无意中碰见了   case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...

  9. SQL Server -- 回忆笔记(四):case函数,索引,子查询,分页查询,视图,存储过程

    SQL Server知识点回忆篇(四):case函数,索引,子查询,分页查询,视图,存储过程 1. CASE函数(相当于C#中的Switch) then '未成年人' else '成年人' end f ...

随机推荐

  1. SQLserver2016对字段是json对象查询

    现在2016内置对json对象的查询支持了.正好项目中用到,做个备忘 如果字段内容是: [{"tagid":"100015","orderid&quo ...

  2. 在虚拟机上的关于Apache(阿帕奇)(3)基于IP访问网站

    这篇随笔是基于IP访问网站,和后面两篇文章基于域名和基于端口一起练习效果更好 基于IP(记得下载httpd服务) 首先使用nmtui命令为网卡添加多个ip地址  输入命令:nmtui  进入下面这个界 ...

  3. html2canvas 生成电子合同书

    首先给合同书父级加个ref 然后用html2canvas 插件 然后html2canvas 坑区来了,生成页面的时候,合同书是很长的.他有一部分是黑色的, 最开始从前辈们了解到是滚至顶部解决偏移.然后 ...

  4. [考试反思]0829NOIP模拟测试33:仰望

    ,160,160,160... 凑和吧,但是莫名开心. 然而这次没有什么想讲的. T1,等比数列求和,XX题,水极,20分钟找规律,找错,快速幂又打错,没检查,10分. T2,想复杂了,想了很久效率很 ...

  5. AutoCad 二次开发 .net 之创建Table

    我使用了COM对象来在cad2018中创建table表格,需要的ObjectArx开发包可以在官网上下载,并且需要使用.netframework4.6的库才行. 项目里除了引用常规的Cad开发dll, ...

  6. CSP-S 95 (sb lsc yy赛)

    sb lsc 终于改完题了!(心力交悴.png)

  7. python_day3(文件处理)

    1.文件处理 #Author:Elson Zeng #data = open("test").read() # f = open("test",'a',enco ...

  8. 类型擦除真的能完全擦除一切信息吗?java 泛型揭秘

    背景 我们都知道泛型本质上是提供类型的"类型参数",它们也被称为参数化类型(parameterized type)或参量多态(parametric polymorphism).其实 ...

  9. 微擎签名出错 invalid signature

    微擎签名出错 错误信息: config:fail,Error: 系统错误,错误码:63002,invalid signature 修改方法: PHP 端 $account_api = WeAccoun ...

  10. pat 1011 World Cup Betting(20 分)

    1011 World Cup Betting(20 分) With the 2010 FIFA World Cup running, football fans the world over were ...