Skip to content

Unified the zh-cn translation of case class #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _zh-cn/glossary/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ language: zh-cn
</div>

* #### 代数数据类型(algebraic data type)
通过提供若干个带有独立构造器的备选项来定义的类型。它一般通过模式匹配的方式来结构类型,在规约语言和函数式编程语言中常见到这个概念。Scala可通过案例类来模拟代数数据类型
通过提供若干个带有独立构造器的备选项来定义的类型。它一般通过模式匹配的方式来结构类型,在规约语言和函数式编程语言中常见到这个概念。Scala可通过样例类来模拟代数数据类型

* #### 备选项(alternative)
match表达式的一个分支,形如 “`case` _pattern_ => _expression_”。备选项的别名是 _案例_(_case_)。
Expand Down
18 changes: 9 additions & 9 deletions _zh-cn/tour/case-classes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tour
title: 案例类(Case Classes)
title: 样例类(Case Classes)
partof: scala-tour

num: 10
Expand All @@ -11,29 +11,29 @@ next-page: pattern-matching
previous-page: multiple-parameter-lists
---

案例类(Case classes)和普通类差不多,只有几点关键差别,接下来的介绍将会涵盖这些差别。案例类非常适合用于不可变的数据。下一节将会介绍他们在[模式匹配](pattern-matching.html)中的应用。
样例类(Case classes)和普通类差不多,只有几点关键差别,接下来的介绍将会涵盖这些差别。样例类非常适合用于不可变的数据。下一节将会介绍他们在[模式匹配](pattern-matching.html)中的应用。

## 定义一个案例类
一个最简单的案例类定义由关键字`case class`,类名,参数列表(可为空)组成:
## 定义一个样例类
一个最简单的样例类定义由关键字`case class`,类名,参数列表(可为空)组成:
```scala mdoc
case class Book(isbn: String)

val frankenstein = Book("978-0486282114")
```
注意在实例化案例类`Book`时,并没有使用关键字`new`,这是因为案例类有一个默认的`apply`方法来负责对象的创建。
注意在实例化样例类`Book`时,并没有使用关键字`new`,这是因为样例类有一个默认的`apply`方法来负责对象的创建。

当你创建包含参数的案例类时,这些参数是公开(public)的`val`
当你创建包含参数的样例类时,这些参数是公开(public)的`val`
```
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
```
你不能给`message1.sender`重新赋值,因为它是一个`val`(不可变)。在案例类中使用`var`也是可以的,但并不推荐这样。
你不能给`message1.sender`重新赋值,因为它是一个`val`(不可变)。在样例类中使用`var`也是可以的,但并不推荐这样。

## 比较
案例类在比较的时候是按值比较而非按引用比较
样例类在比较的时候是按值比较而非按引用比较
```
case class Message(sender: String, recipient: String, body: String)

Expand All @@ -44,7 +44,7 @@ val messagesAreTheSame = message2 == message3 // true
尽管`message2`和`message3`引用不同的对象,但是他们的值是相等的,所以`message2 == message3`为`true`。

## 拷贝
你可以通过`copy`方法创建一个案例类实例的浅拷贝,同时可以指定构造参数来做一些改变。
你可以通过`copy`方法创建一个样例类实例的浅拷贝,同时可以指定构造参数来做一些改变。
```
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")
Expand Down
8 changes: 4 additions & 4 deletions _zh-cn/tour/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ matchTest(1) // one
```
这个`match`表达式是String类型的,因为所有的情况(case)均返回String,所以`matchTest`函数的返回值是String类型。

## 案例类(case classes)的匹配
## 样例类(case classes)的匹配

案例类非常适合用于模式匹配
样例类非常适合用于模式匹配

```scala mdoc
abstract class Notification
Expand All @@ -58,7 +58,7 @@ case class VoiceRecording(contactName: String, link: String) extends Notificatio

```

`Notification` 是一个虚基类,它有三个具体的子类`Email`, `SMS`和`VoiceRecording`,我们可以在这些案例类(Case Class)上像这样使用模式匹配:
`Notification` 是一个虚基类,它有三个具体的子类`Email`, `SMS`和`VoiceRecording`,我们可以在这些样例类(Case Class)上像这样使用模式匹配:

```
def showNotification(notification: Notification): String = {
Expand Down Expand Up @@ -147,5 +147,5 @@ def findPlaceToSit(piece: Furniture): String = piece match {

## 备注

Scala的模式匹配语句对于使用[案例类(case classes)](case-classes.html)表示的类型非常有用,同时也可以利用[提取器对象(extractor objects)](extractor-objects.html)中的`unapply`方法来定义非案例类对象的匹配
Scala的模式匹配语句对于使用[样例类(case classes)](case-classes.html)表示的类型非常有用,同时也可以利用[提取器对象(extractor objects)](extractor-objects.html)中的`unapply`方法来定义非样例类对象的匹配