Skip to content

zh-cn for Scala Tour:Case Classes #1174

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 3 commits into from
Feb 8, 2019
Merged
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
47 changes: 46 additions & 1 deletion _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

discourse: false

Expand All @@ -13,3 +13,48 @@ language: zh-cn
next-page: pattern-matching
previous-page: multiple-parameter-lists
---

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看 Programming Scala 中文版翻译为样例类。这里翻译为案例类有什么特别的缘由么?如果可能,最好能够统一。

我个人倾向样例类,因为字面上其与pattern match有所关联,而且不必创造新的词。但是,如果多数人共识使用案例类,我觉得也行。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们是处理成case类的

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case类我觉得也可以,没有任何误解的可能性。


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

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

当你创建包含参数的案例类时,这些参数是公开(public)的`val`
```
case class Message(sender: String, recipient: String, body: String)
val message1 = Message("[email protected]", "[email protected]", "Ça va ?")

println(message1.sender) // prints [email protected]
message1.sender = "[email protected]" // this line does not compile
```
你不能给`message1.sender`重新赋值,因为它是一个`val`(不可变)。在案例类中使用`var`也是可以的,但并不推荐这样。

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

val message2 = Message("[email protected]", "[email protected]", "Com va?")
val message3 = Message("[email protected]", "[email protected]", "Com va?")
val messagesAreTheSame = message2 == message3 // true
```
尽管`message2`和`message3`引用不同的对象,但是他们的值是相等的,所以`message2 == message3`为`true`。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引用不同的对象

How about this? 參照到不同的物件

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I don't think "參照到不同的物件" conforms to common programming terminologies in Chinese.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got it. It's just the different wording.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「对象」for "object" is mainland China usage, whereas in Taiwan it is usually 「物件」.


## 拷贝
你可以通过`copy`方法创建一个案例类实例的浅拷贝,同时可以指定构造参数来做一些改变。
```
case class Message(sender: String, recipient: String, body: String)
val message4 = Message("[email protected]", "[email protected]", "Me zo o komz gant ma amezeg")
val message5 = message4.copy(sender = message4.recipient, recipient = "[email protected]")
message5.sender // [email protected]
message5.recipient // [email protected]
message5.body // "Me zo o komz gant ma amezeg"
```
上述代码指定`message4`的`recipient`作为`message5`的`sender`,指定`message5`的`recipient`为"[email protected]",而`message4`的`body`则是直接拷贝作为`message5`的`body`了。