1.
1
|
def
a
_=
(v
:
Int)
=
arr(
0
)
=
v
|
2.
1
2
|
val
arr
=
Array(
1
,
2
,
3
,
4
,
5
)
val
sum
=
arr.reduceLeft((a
:
Int, b
:
Int)
=
> a + b)
|
3.
1
|
private
def
splitName()
=
name.split(
" "
)
|
4. 这应该算语法糖
1
|
var
m
=
1
to
10
|
5.
1
|
args.foreach(arg
=
> greeting +
=
(arg +
" "
))
|
6. 对象 方法 参数
1
2
3
4
5
6
7
|
object
SharkTest{
def
m (i
:
Int)
=
i;
def
main(args
:
Array[String]) {
var
s
=
SharkTest m
1
//没错 scala语法可以这样写
}
}
|
7.
1
2
3
4
|
def
s(x
:
Int)(y
:
Int)(z
:
Int)
=
x+y+z;
def
main(args
:
Array[String]) {
var
m
=
s(
1
)(
2
)(
3
);
}
|
8.从shark源码中看到的,我改写了简单的例子
1
2
3
4
5
6
7
8
9
10
|
class
A ;
class
B
extends
A
class
C
extends
A
main
:
var
a
=
new
B;
a
match
{
case
_
:
B
=
> {
//这里什么都不做的意思}
case
_
:
C
=
> {}
case
_
=
> println(
7
)
}
|
9.
1
2
3
|
val
f
=
{}
//什么都没有
val
f
=
{println(
1
);
3
}
//执行完打印,f值仍然为3
val
f
=
3
;
|
10 Scala使用下划线表示不同上下文中的不同事物,你通常可以把它看作是一个没有命名的神奇通配符
1
2
|
def
adder(m
:
Int, n
:
Int)
=
m + n
val
add
2
=
adder(
2
,
_:
Int)
|
长期更新,并求网友贡献。。
本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/yjplxq/1414398,如需转载请自行联系原作者