Scala学习(一)——基础、控制结构和函数

一、基础

1. 变量

1.1 声明变量

var 变量名:变量类型 =

1.2 声明常量

val 常量名:变量类型 =

2. 变量类型

  • Byte
  • Short
  • Int(整数默认)
  • Long
  • Float
  • Double(小数默认)
  • Char
  • Boolean
  • java.lang.String

前面8种基本类型属于scala._
类型支持自动推断
自动类型提升和Java相同

3. 运算符

除了没有++、–运算符外和Java一样

二、控制结构

1. 条件结构

1.1 if

同Java

在Scala中,if…else…表达式有值,如下:
val s = if(x > 0) 1 else -1
效果等同于 if(x > 0) s = 1 else s = -1,但第一种好一点,因为第二种s必须是var

1.2 match

Scala中没有switch,但有比switch更好的模式匹配。

变量 match {
	case=> 处理语句
	case=> 处理语句
	case _ => 处理语句
}

注意:

  1. case _类似于java的case default
  2. match与if一样,也是表达式,不是语句,也是有值的

2. 循环结构

2.1 while

while(条件){
//循环体
}

2.2 do … while

do{
//循环体
}while(条件)

2.3 for

 for(i <- 0 to 9){
 //执行10次循环体
 }
 for(i <- 0 until 10){
 //执行10次循环体
 }
 for(i <- 0 to 9 by 2){
 //循环体,步长为2
 }

嵌套for循环:

scala> for(i <- 1 to 9; j <- 1 to i){print(i + "*" + j + "=" + i*j + "\t"); if(i==j){println()}}
1*1=1
2*1=2   2*2=4
3*1=3   3*2=6   3*3=9
4*1=4   4*2=8   4*3=12  4*4=16
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81

if守卫

scala> for(i <- 0 to 9 if i % 2 == 0){print(i + "\t")}
0       2       4       6       8

2.4 break 和continue

Scala中没有break和continue

2.5 for推导式(yield)

如果for循环以yield开头,则该循环会构造出一个集合,每次迭代会生成集合中的一个值:

scala> for(c <- "hello"; i <- 0 to 2) yield c
res20: String = hhheeellllllooo

for推导式生成的集合的类型和第一个生成器的类型是兼容的。

scala> for(i <- 0 to 2; c <- "hello") yield c
res19: scala.collection.immutable.IndexedSeq[Char] = Vector(h, e, l, l, o, h, e, l, l, o, h, e, l, l, o)
scala> for(c <- "hello"; i <- 0 to 2) yield c
res20: String = hhheeellllllooo

三、函数

1. 函数声明

函数三要素:函数名、参数列表、返回值类型

def 函数名(参数列表): 返回值类型{
	//函数体
}

注意:递归函数必须指定返回值类型

2. 默认参数和带名参数

scala> def test(left: String = "[", word: String, right: String = "]"){println(left + word+ right)}
test: (left: String, word: String, right: String)Unit

scala> test(word = "笑傲江湖")
[笑傲江湖]

scala> test("{{{", "笑傲江湖", "}}}")
{{{笑傲江湖}}}

3. 变长参数

scala> def myprint(args: String*){for(s <- args){println(s)}}
myprint: (args: String*)Unit

scala> myprint("射雕英雄传")
射雕英雄传

scala> myprint("射雕英雄传", "神雕侠侣","倚天屠龙记")
射雕英雄传
神雕侠侣
倚天屠龙记

4. 懒值

当val被声明为lazy时,它的初始化将被推迟,直到我们首次对它取值。例如:

lazy val words = scala.io.Source.fromFile("/home/word.txt").mkString

5. 异常

5.1 抛出异常

Scala没有已检查异常,不需要声明函数或方法会抛出某种异常,但抛出的对象必须是java.lang.Throwable的子类。

throw new IllegalArgumentException("x shoule not be negative")

throw 表达式有特殊类型Nothing。

5.2 捕获异常

try{
	//可能出错的代码
} catch {
case ex1: RuntimeException => ex1.printStackTrace()
case ex2: Exception => ex2.printStackTrace()
case _ => println("other exception")
}

6. 匿名函数

参数列表 => 函数体

scala内置了23个匿名函数对象Function0~Function22

7. 柯里化函数

柯里化函数指将接受多个参数的函数,改造为接受一个参数的函数,并且返回一个函数对象的过程
原函数 def klh(x:Int, y:Int, z:Int) = {x + y + z}
柯里化:

scala> def klh(x:Int) = { y:Int => z:Int => x + y + z }
klh: (x: Int)Int => (Int => Int)

四、高阶函数

1. 参数推断

scala> 1 to 5
res28: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)

scala> res28.foreach(println)
1
2
3
4
5

2. 函数闭包

下例中函数fun则是一个闭包函数,因为它引用到外面定义的变量n

scala> def func(): Unit ={ var n = 1; val fun = (x:Int) => x + n; println(fun(100)); n = 100; println(fun(100));}
func: ()Unit

scala> func
101
200
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值