1. 程式人生 > >scala程式設計系列(1)-scala程式設計入門初步

scala程式設計系列(1)-scala程式設計入門初步

1.scala直譯器

scala與python一樣,可以在終端以直譯器方式互動,只需在終端輸入scala即可

[email protected]:~$ scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 

在直譯器上可以進行一些簡單的互動,如下:
scala> 1+2
res0: Int = 3

scala> 5+6
res1: Int = 11

scala> 2+3
res2: Int = 5

scala> res3
res4: Int = 5
 

其資訊的意思為:

res:自動產生的接收結果的變數,並自動編號為res0,res1,res2。冒號(:)後面Int代表型別,等號(=)後面為表示式結果

2.變數定義

scala裡有兩種變數,val和var。val類似於Java裡的final變數,一旦被初始化了就不能再被賦值。var如同普通變數,可以在生命週期內多次賦值。

scala> val msg="Hello World"
msg: String = Hello World

scala> msg="Hello World again"
<console>:8: error: reassignment to val
       msg="Hello World again"

scala> var msg2="Hello World"
msg2: String = Hello World

scala> msg2="Hello World again"
msg2: String = Hello World again

scala> msg2
res1: String = Hello World again

scala裡面的變數型別一般都是與java相對應的,在賦值時,它可以自動識別為某種型別,我們也可以自己定義變數型別。定義的方式為在變數的後面加上冒號(:)和型別,如:
scala> val msg3:String="Hello World again"
msg3: String = Hello World again

3.函式定義

scala> def max(x: Int, y: Int): Int = {
     | if (x > y) x
     | else y
     | }
max: (x: Int, y: Int)Int

scala> max(5,6)
res2: Int = 6

函式的基本結構如下:


但是,這似乎有點複雜,我們可以省略一些不必要的,如省略掉花括號

scala> def max2(x: Int,y: Int) = if (x>y) x else y
max2: (x: Int, y: Int)Int

scala> max2(5,6)
res3: Int = 6
另外還有一種是不帶引數,也不返回有用結果的函式定義
scala> def greet() = println("Hello World!")
greet: ()Unit

scala> greet
Hello World!

scala> greet()
Hello World!

4.編寫scala指令碼

不帶引數的指令碼

[email protected]:~/Mywork/scala$ cat hello.scala 
println("Hello world,from a script!")

[email protected]:~/Mywork/scala$ scala hello.scala 
Hello world,from a script!
帶引數的指令碼
[email protected]:~/Mywork/scala$ cat helloarg.scala 
println("Hello, "+args(0) +"!")

[email protected]:~/Mywork/scala$ scala helloarg.scala Earth
Hello, Earth!

5.while迴圈,if判斷

while迴圈

[email protected]:~/Mywork/scala$ scala printargs.scala Scala is fun
Scala
is
fun

[email protected]:~/Mywork/scala$ cat printargs.scala 
var i = 0
while (i < args.length) {
    println(args(i))
    i += 1
}
更進一步,帶if的,且格式化輸出。
[email protected]:~/Mywork/scala$ scala echoargs.scala Scala is even more fun
Scala is even more fun
[email protected]:~/Mywork/scala$ cat echoargs.scala 
var i = 0
while (i < args.length){
    if(i != 0)
      print(" ")
    print(args(i))
    i += 1
}
println()

6.for,foreach

用foreach列印引數,非常簡潔

[email protected]:~/Mywork/scala$ scala pa.scala Friday is beautiful
Friday
is
beautiful

[email protected]:~/Mywork/scala$ cat pa.scala 
<pre name="code" class="plain">args.foreach(arg => println(arg))

上面pa.scala的完全寫法是

args.foreach((arg:String) => println(arg))
上面foreach括號內是一個函式,其函式標準格式如下(右鍵頭指的方向有點偏)


for迴圈示例

[email protected]:~/Mywork/scala$ scala forargs.scala The Dragon Boat Festival is comming
The
Dragon
Boat
Festival
is
comming
[email protected]:~/Mywork/scala$ cat forargs.scala 
for (arg <- args)
    println(arg)