序 ........................................................................................................XXIV
致谢 .....................................................................................................XXVI
引言 .....................................................................................................XXIX
第1章 一门可伸缩的语言 ..........................................................................1
1.1 一门按需伸缩的语言 ..................................................................... 2
1.2 是什么让Scala能屈能伸 ............................................................. 7
1.3 为什么要用Scala .......................................................................11
1.4 Scala寻根 ..................................................................................... 17
1.5 结语............................................................................................. 19
第2章 Scala入门 ....................................................................................20
第1步 使用Scala解释器 ..................................................................... 21
第2步 定义变量................................................................................. 22
第3步 定义函数................................................................................. 24
第4步 编写Scala脚本 ......................................................................... 26
第5步 用while做循环;用if做判断 .................................................... 27
第6步 用foreach和for遍历 ................................................................. 30
结语...................................................................................................... 32
第3章 Scala入门(续) ..........................................................................33
第7步 用类型参数化数组.................................................................. 33
第8步 使用列表 ................................................................................. 38
第9步 使用元组 ................................................................................. 42
第10步使用集和映射 ........................................................................... 43
第11步 识别函数式编程风格 ............................................................. 48
第12步 从文件读取文本行 ................................................................ 51
结语...................................................................................................... 55
第4章 类和对象 ......................................................................................56
4.1 类、字段和方法........................................................................... 56
4.2 分号推断...................................................................................... 61
4.3 单例对象...................................................................................... 62
4.4 Scala应用程序 ............................................................................. 65
4.5 App特质 ....................................................................................... 68
4.6 结语 ............................................................................................. 68
第5章 基础类型和操作 ............................................................................69
5.1 一些基础类型 .............................................................................. 69
5.2 字面量.......................................................................................... 70
5.3 字符串插值 .................................................................................. 77
5.4 操作符即方法 .............................................................................. 78
5.5 算术操作...................................................................................... 81
5.6 关系和逻辑操作........................................................................... 83
5.7 位运算操作 .................................................................................. 85
5.8 对象相等性 .................................................................................. 86
5.9 操作符优先级和结合性 ............................................................... 88
5.10 富包装类.................................................................................... 91
5.11 结语............................................................................................ 91
第6章 函数式对象 ...................................................................................93
6.1 Rational类的规格定义 ................................................................. 93
6.2 构建Rational ................................................................................ 94
6.3 重新实现toString方法 .................................................................. 96
6.4 检查前置条件.............................................................................. 97
6.5 添加字段...................................................................................... 98
6.6 自引用.........................................................................................100
6.7 辅助构造方法 .............................................................................101
6.8 私有字段和方法..........................................................................102
6.9 定义操作符 .................................................................................104
6.10 Scala中的标识符 .......................................................................106
6.11 方法重载 ...................................................................................108
6.12 隐式转换...................................................................................111
6.13 注意事项...................................................................................112
6.14 结语 ..........................................................................................112
第7章 内建的控制结构 .......................................................................... 114
7.1 if表达式 ......................................................................................115
7.2 while循环 ....................................................................................116
7.3 for表达式 ....................................................................................119
7.4 用try表达式实现异常处理 ..........................................................125
7.5 match表达式 ...............................................................................129
7.6 没有break和continue的日子 ........................................................131
7.7 变量作用域.................................................................................134
7.8 对指令式代码进行重构 ..............................................................137
7.9 结语 ............................................................................................139
第8章 函数和闭包 .................................................................................140
8.1 方法 ............................................................................................140
8.2 局部函数.....................................................................................142
8.3 一等函数.....................................................................................144
8.4 函数字面量的简写形式 ..............................................................147
8.5 占位符语法 .................................................................................147
8.6 部分应用的函数..........................................................................149
8.7 闭包 .................................................................