@@ -19,75 +19,143 @@ permalink: "/zh-cn/scala3/book/:title.html"
1919Scala 类、样例类、traits、枚举和对象都可以包含方法。
2020简单方法的语法如下所示:
2121
22+ {% tabs method_1 %}
23+ {% tab 'Scala 2 and 3' for=method_1 %}
24+
2225``` scala
2326def methodName (param1 : Type1 , param2 : Type2 ): ReturnType =
2427 // the method body
2528 // goes here
2629```
2730
31+ {% endtab %}
32+ {% endtabs %}
33+
2834这有一些例子:
2935
36+ {% tabs method_2 %}
37+ {% tab 'Scala 2 and 3' for=method_2 %}
38+
3039``` scala
3140def sum (a : Int , b : Int ): Int = a + b
3241def concatenate (s1 : String , s2 : String ): String = s1 + s2
3342```
3443
44+ {% endtab %}
45+ {% endtabs %}
46+
3547您不必声明方法的返回类型,因此如果您愿意,可以像这样编写这些方法:
3648
49+ {% tabs method_3 %}
50+ {% tab 'Scala 2 and 3' for=method_3 %}
51+
3752``` scala
3853def sum (a : Int , b : Int ) = a + b
3954def concatenate (s1 : String , s2 : String ) = s1 + s2
4055```
4156
57+ {% endtab %}
58+ {% endtabs %}
59+
4260这是你如何调用这些方法:
4361
62+ {% tabs method_4 %}
63+ {% tab 'Scala 2 and 3' for=method_4 %}
64+
4465``` scala
4566val x = sum(1 , 2 )
4667val y = concatenate(" foo" , " bar" )
4768```
4869
70+ {% endtab %}
71+ {% endtabs %}
72+
4973这是一个多行的方法:
5074
75+ {% tabs method_5 class=tabs-scala-version %}
76+ {% tab 'Scala 2' for=method_5 %}
77+
78+ ``` scala
79+ def getStackTraceAsString (t : Throwable ): String = {
80+ val sw = new StringWriter
81+ t.printStackTrace(new PrintWriter (sw))
82+ sw.toString
83+ }
84+ ```
85+
86+ {% endtab %}
87+ {% tab 'Scala 3' for=method_5 %}
88+
5189``` scala
5290def getStackTraceAsString (t : Throwable ): String =
5391 val sw = new StringWriter
5492 t.printStackTrace(new PrintWriter (sw))
5593 sw.toString
5694```
5795
96+ {% endtab %}
97+ {% endtabs %}
98+
5899方法参数也可以具有默认值。
59100在此示例中,` timeout ` 参数的默认值为 ` 5000 ` :
60101
102+ {% tabs method_6 %}
103+ {% tab 'Scala 2 and 3' for=method_6 %}
104+
61105``` scala
62106def makeConnection (url : String , timeout : Int = 5000 ): Unit =
63107 println(s " url= $url, timeout= $timeout" )
64108```
65109
110+ {% endtab %}
111+ {% endtabs %}
112+
66113由于方法声明中提供了默认的 ` 超时 ` 值,因此可以通过以下两种方式调用该方法:
67114
115+ {% tabs method_7 %}
116+ {% tab 'Scala 2 and 3' for=method_7 %}
117+
68118``` scala
69119makeConnection(" https://localhost" ) // url=http://localhost, timeout=5000
70120makeConnection(" https://localhost" , 2500 ) // url=http://localhost, timeout=2500
71121```
72122
123+ {% endtab %}
124+ {% endtabs %}
125+
73126Scala 还支持在调用方法时使用 _ 命名参数_ ,因此如果您愿意,也可以像这样调用该方法:
74127
128+ {% tabs method_8 %}
129+ {% tab 'Scala 2 and 3' for=method_8 %}
130+
75131``` scala
76132makeConnection(
77133 url = " https://localhost" ,
78134 timeout = 2500
79135)
80136```
81137
138+ {% endtab %}
139+ {% endtabs %}
140+
82141当多个方法参数具有相同的类型时,命名参数特别有用。
83142乍一看,使用此方法,您可能想知道哪些参数设置为 ` true ` 或 ` false ` :
84143
144+ {% tabs method_9 %}
145+ {% tab 'Scala 2 and 3' for=method_9 %}
146+
85147``` scala
86148engage(true , true , true , false )
87149```
88150
151+ {% endtab %}
152+ {% endtabs %}
153+
89154如果没有IDE的帮助,那段代码可能很难阅读,但这个代码要明显得多:
90155
156+ {% tabs method_10 %}
157+ {% tab 'Scala 2 and 3' for=method_10 %}
158+
91159``` scala
92160engage(
93161 speedIsSet = true ,
@@ -96,12 +164,17 @@ engage(
96164 turnedOffParkingBrake = false
97165)
98166```
167+ {% endtab %}
168+ {% endtabs %}
99169
100170## 扩展方法
101171
102172_ 扩展方法_ 允许您向封闭类添加新方法。
103173例如,如果要将两个名为 ` hello ` 和 ` aloha ` 的方法添加到 ` String ` 类中,请将它们声明为扩展方法:
104174
175+ {% tabs extension0 %}
176+ {% tab 'Scala 3 Only' %}
177+
105178``` scala
106179extension (s : String )
107180 def hello : String = s " Hello, ${s.capitalize}! "
@@ -111,13 +184,19 @@ extension (s: String)
111184" friend" .aloha // "Aloha, Friend!"
112185```
113186
187+ {% endtab %}
188+ {% endtabs %}
189+
114190` extension ` 关键字声明了括号内的参数将定义一个或多个扩展方法。
115191如此示例所示,可以在扩展方法体中使用 ` String ` 类型的参数 ` s ` 。
116192
117193下一个示例演示如何将 ` makeInt ` 方法添加到 ` String ` 类。
118194在这里,` makeInt ` 采用一个名为 ` radix ` 的参数。
119195该代码不考虑可能的字符串到整数转换错误,但跳过细节,示例显示了它的工作原理:
120196
197+ {% tabs extension %}
198+ {% tab 'Scala 3 Only' %}
199+
121200``` scala
122201extension (s : String )
123202 def makeInt (radix : Int ): Int = Integer .parseInt(s, radix)
@@ -127,7 +206,10 @@ extension (s: String)
127206" 100" .makeInt(2 ) // Int = 4
128207```
129208
130- ## See also
209+ {% endtab %}
210+ {% endtabs %}
211+
212+ ## 参见
131213
132214Scala方法可以更强大:它们可以采用类型参数和上下文参数。
133215它们在[ 领域建模] [ data-1 ] 一节中有详细介绍。
0 commit comments