File tree Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
layout : tour
3
- title : Named Arguments
3
+ title : 命名参数
4
4
5
5
discourse : false
6
6
@@ -13,3 +13,22 @@ language: zh-cn
13
13
next-page : packages-and-imports
14
14
previous-page : default-parameter-values
15
15
---
16
+
17
+ 当调用方法时,实际参数可以通过其对应的形式参数的名称来标记:
18
+
19
+ ``` tut
20
+ def printName(first: String, last: String): Unit = {
21
+ println(first + " " + last)
22
+ }
23
+
24
+ printName("John", "Smith") // Prints "John Smith"
25
+ printName(first = "John", last = "Smith") // Prints "John Smith"
26
+ printName(last = "Smith", first = "John") // Prints "John Smith"
27
+ ```
28
+ 注意使用命名参数时,顺序是可以重新排列的。 但是,如果某些参数被命名了,而其他参数没有,则未命名的参数要按照其方法签名中的参数顺序放在前面。
29
+
30
+ ``` tut:fail
31
+ printName(last = "Smith", "john") // error: positional after named argument
32
+ ```
33
+
34
+ 注意调用 Java 方法时不能使用命名参数。
You can’t perform that action at this time.
0 commit comments