Scala: Case classes
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的更多相关文章
- Scala:case class
Scala:case class 1.Scala中class.object.case class.case object区别 1.1 class 和 object 关系 1.2 case class ...
- Programming In Scala笔记-第十五章、Case Classes和模式匹配
本章主要分析case classes和模式匹配(pattern matching). 一.简单例子 接下来首先以一个包含case classes和模式匹配的例子来展开本章内容. 下面的例子中将模拟实现 ...
- 学习Scala: 初学者应该了解的知识
Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...
- Scala:使用Sublime开发Scala
Scala:使用Sublime开发Scala 第一步:[Tools][Build System][New Build System] 第二步:在打开的新文件中输入: { //"cmd&quo ...
- 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 ...
- Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC
Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC pom.xml添加依赖 Java:方式一(亲测实用) 方式二:Scala 方式三:Java PhoenixJDBCU ...
- 客户端,Scala:Spark查询Phoenix
客户端,Scala:Spark查询Phoenix 1.pom.xml 2.配置文件 2.1config.properties 2.2MyConfig 3.entity实体(与phoenix中的tabl ...
- android switch语句报错:case expressions must be constant expressions
今天无意中碰见了 case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...
- SQL Server -- 回忆笔记(四):case函数,索引,子查询,分页查询,视图,存储过程
SQL Server知识点回忆篇(四):case函数,索引,子查询,分页查询,视图,存储过程 1. CASE函数(相当于C#中的Switch) then '未成年人' else '成年人' end f ...
随机推荐
- 获得shell的几种姿势
windows提权 1.通过sqlmap连接mysql获取shell (1)直接连接数据库 sqlmap.py -d "mysql://root:123456@127.0.0.1:3306/ ...
- ES入门宝典(详细截图版)
本文使用版本基于elasticsearch-6.4.0 1.什么是ES? 官网: https://www.elastic.co/products/elasticsearch 中文官网:https:/ ...
- Android 开源库 GitHub 托管
本文微信公众号「AndroidTraveler」首发. 背景 之前给大家写过一篇文章 Android 上传开源项目到 jcenter 实战踩坑之路,分享了上传开源项目到 jcenter 上面的一些踩坑 ...
- WeihanLi.Npoi 近期更新
WeihanLi.Npoi 近期更新 Intro 最近对我的 NPOI 扩展做了一些改变,一方面提高性能,一方面修复bug,增加一些新的功能来让它更加好用,前几天发布了 1.5.0 版本,下面来介绍一 ...
- [校内自测 NOIP模拟题] chenzeyu97要请客(单调栈)
题目描述 chenzeyu97的家可以看成是一个n*m的矩阵,每块区域都有独一无二的海拔高度h(h>0)!其最大值为n*m. 现在他要选择一个子矩阵摆放一张桌子,在他眼里,这样摆放桌子的美观度为 ...
- IntelliJ IDEA自动部署项目至远程服务器与传统部署项目至远程服务器的区别
每次开发Java项目时,对于所有Java开发人员来说,最枯燥的不是修改代码,而是实时将自己的代码上传至远程服务器,进行测试或者部署,本人最初开发也是这样,通过使用Xshell 5,WinSCP等工具对 ...
- PHP经典算法题
1.百钱买百鸡 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱. 分析:估计现在小学生都能手工推 ...
- go-micro+php+consul简单的微服实现
首先我们用go-micro构建一个服务.(关于go-micro的使用可以参照官方实例或者文档) //新建一个微服务 micro new --type "srv" user-srv ...
- [内部类] java笔记之内部类
1.内部类的分类 2.成员内部类的定义格式 3.一旦使用了内部类,那么生成的class文件长啥样? 其中Body是外部类,Heart是Body的内部类,所以中间有个美元符号$,所以给类进行命名时,不要 ...
- 学习Java第一步:安装Intellij IDEA和JDK
注:其实真正学习一门新语言的第一步并不是安装开发工具,我是C#转JAVA,有一点编程经验了,所以可以直接跳过前面几步,直接上IDE. 1.下载IntelliJ IDEA [官网] http://www ...