Scala:Method 小技巧,忽略result type之后的等号

 1 var x = 0
 2 
 3 def IncreaseOne(): Int = {
 4   x += 1
 5   x
 6 }
 7 
 8 def IncreaseOne() = {
 9   x += 1
10   x
11 }
12 
13 def IncreaseOne = {
14   x += 1
15   x
16 }
17 
18 
19 def IncreaseOne(): Unit = {
20   x += 1
21   x
22 }
23 
24 def IncreaseOne() {
25   x += 1
26   x
27 }
28 
29 
30 def IncreaseOne = {
31   x += 1
32   x
33 }

上文中,前三个方法的定义是等同的,后三个也是等同的。规则非常简单,如果方法的返回类型为Unit,则可以忽略result type 和 = 号。如果方法没有参数,括号也可以忽略。

posted on 2015-01-04 21:49  幸福框架  阅读(707)  评论(0编辑  收藏  举报

导航

我要啦免费统计