Skip to content

Add Chinese translation of default-parameter-values. #1192

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 2 commits into from
Jan 16, 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
36 changes: 35 additions & 1 deletion _zh-cn/tour/default-parameter-values.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tour
title: Default Parameter Values
title: 默认参数值

discourse: false

Expand All @@ -13,3 +13,37 @@ language: zh-cn
next-page: named-arguments
previous-page: annotations
---

Scala具备给参数提供默认值的能力,这样调用者就可以忽略这些具有默认值的参数。

```tut
def log(message: String, level: String = "INFO") = println(s"$level: $message")

log("System starting") // prints INFO: System starting
log("User not found", "WARNING") // prints WARNING: User not found
```

上面的参数level有默认值,所以是可选的。最后一行中传入的参数`"WARNING"`重写了默认值`"INFO"`。在Java中,我们可以通过带有可选参数的重载方法达到同样的效果。不过,只要调用方忽略了一个参数,其他参数就必须要带名传入。

```tut
class Point(val x: Double = 0, val y: Double = 0)

val point1 = new Point(y = 1)
```
这里必须带名传入`y = 1`。

注意从Java代码中调用时,Scala中的默认参数则是必填的(非可选),如:

```tut
// Point.scala
class Point(val x: Double = 0, val y: Double = 0)
```

```java
// Main.java
public class Main {
public static void main(String[] args) {
Point point = new Point(1); // does not compile
}
}
```