-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
layout: tour | ||
title: Default Parameter Values | ||
title: 默认参数值 | ||
|
||
discourse: false | ||
|
||
|
@@ -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中的默认参数就不是可选的了,如: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注意从Java代码中调用时,Scala中的默认参数则是必填的(非可选)。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion, changed. |
||
|
||
```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 | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面的参数
level
有个默认值 -> 上面的参数level
有默认值最后一行中传入的参数
"WARNING"
重写了默认的"INFO"
-> 最后一行中传入的参数"WARNING"
重写了默认值"INFO"
Java中可以重载方法的地方,都可以通过带有可选参数的方法达到同样的效果 -> 在Java中,我们可以通过带有可选参数的重载方法达到同样的效果
不过,调用方只要忽略了一个参数,其他参数必须要带名传入。-> 不过,只要调用方忽略了一个参数,其他参数就必须要带名传入。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, updated.