Tour of Scala

# 基础

Language

## 在浏览器上尝试Scala

1. 打开Scastie
2. 在左侧窗格中粘贴`println("Hello, world!")`
3. 点击”Run”按钮，输出将展现在右侧窗格中。

## 表达式

``````1 + 1
``````

``````println(1) // 1
println(1 + 1) // 2
println("Hello!") // Hello!
println("Hello," + " world!") // Hello, world!
``````

### 常量（`Values`）

``````val x = 1 + 1
println(x) // 2
``````

``````x = 3 // This does not compile.
``````

``````val x: Int = 1 + 1
``````

### 变量

``````var x = 1 + 1
x = 3 // This compiles because "x" is declared with the "var" keyword.
println(x * x) // 9
``````

``````var x: Int = 1 + 1
``````

## 代码块（Blocks）

``````println({
val x = 1 + 1
x + 1
}) // 3
``````

## 函数

``````(x: Int) => x + 1
``````

`=>`的左边是参数列表，右边是一个包含参数的表达式。

``````val addOne = (x: Int) => x + 1
``````

``````val add = (x: Int, y: Int) => x + y
``````

``````val getTheAnswer = () => 42
``````

## 方法

``````def add(x: Int, y: Int): Int = x + y
``````

``````def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier
``````

``````def name: String = System.getProperty("user.name")
println("Hello, " + name + "!")
``````

``````def getSquareString(input: Double): String = {
val square = input * input
square.toString
}
println(getSquareString(2.5)) // 6.25
``````

## 类

``````class Greeter(prefix: String, suffix: String) {
def greet(name: String): Unit =
println(prefix + name + suffix)
}
``````

`greet`方法的返回类型是`Unit`，表明没有什么有意义的需要返回。它有点像Java和C语言中的`void`。（不同点在于每个Scala表达式都必须有值，事实上有个`Unit`类型的单例值，写作`()`，它不携带任何信息）

``````val greeter = new Greeter("Hello, ", "!")
greeter.greet("Scala developer") // Hello, Scala developer!
``````

## 样例类

Scala有一种特殊的类叫做样例类（case class）。默认情况下，样例类一般用于不可变对象，并且可作值比较。你可以使用`case class`关键字来定义样例类。

``````case class Point(x: Int, y: Int)
``````

``````val point = Point(1, 2)
val anotherPoint = Point(1, 2)
val yetAnotherPoint = Point(2, 2)
``````

``````if (point == anotherPoint) {
println(s"\$point and \$anotherPoint are the same.")
} else {
println(s"\$point and \$anotherPoint are different.")
} // Point(1,2) and Point(1,2) are the same.

if (point == yetAnotherPoint) {
println(s"\$point and \$yetAnotherPoint are the same.")
} else {
println(s"\$point and \$yetAnotherPoint are different.")
} // Point(1,2) and Point(2,2) are different.
``````

## 对象

``````object IdFactory {
private var counter = 0
def create(): Int = {
counter += 1
counter
}
}
``````

``````val newId: Int = IdFactory.create()
println(newId) // 1
``````

## 特质

``````trait Greeter {
def greet(name: String): Unit
}
``````

``````trait Greeter {
def greet(name: String): Unit =
println("Hello, " + name + "!")
}
``````

``````class DefaultGreeter extends Greeter

class CustomizableGreeter(prefix: String, postfix: String) extends Greeter {
override def greet(name: String): Unit = {
println(prefix + name + postfix)
}
}

val greeter = new DefaultGreeter()
greeter.greet("Scala developer") // Hello, Scala developer!

val customGreeter = new CustomizableGreeter("How are you, ", "?")
customGreeter.greet("Scala developer") // How are you, Scala developer?
``````

## 主方法

``````object Main {
def main(args: Array[String]): Unit =
println("Hello, Scala developer!")
}
``````