版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33096883/article/details/83620592
scala
scalable language, 可伸缩语言。Scala是一门综合了面向对象和函数式编程概念的静态类型的编程语言。
优点
- 函数式编程(函数的地位是和整数、字符串等是相同的)
- 高阶面向对象(每个值都是对象,每个操作都是方法调用)
- 类型推断
运行
- 标准的java平台
- 与所有java类库无缝协作
语法
循环
args.foreach((arg: String) => println(arg))
args.foreach(arg => println(arg))
/*
foreach的参数是函数字面量(function literal),语法:
(x: Int, y: Int) => x+y
*/
for (arg <- args)
println(arg)
for (i <- 0 to 2)
println(i)
for (
file <- files
if file.isFile
if file.getName.endWith(".scala")
line <- lines
)println(file + ": " + line)
match
val firstArg = if (!args.isEmpty) args(0) else ""
val friend =
firstArg match {
case "salt" => "pepper"
case "eggs" => "bacon"
case _ => "huh?"
}
println(friend)
/*
$scala test.scala eggs
bacon
$scala test.scala xxx
huh?
*/
函数
scala支持一等函数。不仅可以定义并调用,还可以用匿名的字面量来编写函数并将它们作为值传递
// 函数定义
def max(x: Int, y: Int): Int = {
if (x>y) x
else y
}
// 字面量
var inc = (x: Int) => x+1
inc(1) // 2
// 占位符_ 用来表示一个或多个参数,可以匹配任何对象,
// 只要满足每个参数只在函数字面量出现一次即可
nums.filter((x: Int) => x > 0)
nums.filter(x => x > 0)
nums.filter(_ > 0)
nums.sortWith(_ > 0) // decent
// _ + _ 将会展开成一个接受二个参数的函数自面量
val f = (_: Int) + (_: Int)
f(5,10) // 15
// _*
def echo(args:String*) =
for (arg <- args) println(arg)
echo("Hello", "world") // right
val arr = Array("What", "???")
echo(arr) // error
// 将arr的每个元素作为参数传给echo,而不是将所有蒜素放在一起作为单个实参传入
echo(arr: _*) // right
// 函数参数
def filesMatching(qurey: String, matcher: (String, String) => Boolean) = {
for (file <- files; if matcher(file.getName, query))
yield file
}
def filesEnding(query: String) =
filesMatching(query, _.endsWith(_))
/*
字面量_.endWith(_)等价于
(fileName: String, query: String) => fileName.endWith(query)
因为这二个参数在函数体分别只用到一次,可以用占位符语法来写
*/
def filesMatching(matcher: String => Boolean) = {
for (file <- files; if matcher(file.getName))
yield file
}
def filesEnding(query: String) =
filesMatching(_.endsWith(query))
数据类型和类
基础类型
Byte、Short、Int、Long、Char、String、Float、Double、Boolean。
String 插值
// s插值器对内嵌的表达式求值
val name = "qihao"
println(s"Hello, $name, ${1+2}") //Hello, qihao, 3
println(raw"No\\\space") // No\\\space
println(f"${math.Pi}%.5f") // 3.14159
变量
- var 可以修改
- val 不可修改
- 函数式编程倾向于用val
- 多行输入用 | 连接,类似于c的\
数组
- Array 可以修改
val s = new Array[String](3)
s(0) = "hello" // 底层会调用以下方法
s.update(0, "Hello")
val s2 = Array("0", "1", "2") // 底层会调用以下方法
val s3 = Array.apply("3", "4", "5")
/*
会调用apply()方法,创建数据并返回,类似与java类中有一个apply的静态方法
*/
- List 不可修改
// ::: 用于拼接, :: 在已有列表的最前面添加元素
val x = List(1, 2)
val y = List(3, 4)
val z = x ::: y // List(1, 2, 3, 4)
val zz = 0 :: z // List(0,1,2,3,4)
zz.map(s => s+1) // List(1, 2, 3, 4, 5)
tuple,set,map
// tuple 不可变,可以容纳不同类型的元素
val pair = (100, "hello")
println(pair._1)
// set
var set = Set("hello", "world")
set += "qihao" // Set(hello, world, qihao)
// map
val map = Map(1->"hello", 2->"world")
map(2) // world
类与对象
单例对象
scala 类中不允许有静态成员,于是有了singleton,关键字object
// 单例对象和类公用一个名字,单例对象称为这个类的伴生对象,类叫做单例对象的伴生类。它们之间可以互相访问对方的私有成员。
// 可以将单例对象当做用于安置那些用java时打算编写的静态方法。
class Test{
}
object Test {
def xxx() = {}
}
Test.xxx()
// 重载*符号
def * () {}
// ++ 拼接二个数组
def above(that: Element): Element =
new ArrayElement(this.contents ++ that.contents)
// zip 从它的二个操作元中取出对应元素,责成一个pair的数组
new ArrayElement {
for ( (line1, line2) <- this.contents zip that.contents
yield line1 + line2
}
trait
可以用extends或with关键字混入(mix in)类中,并非继承。
trait xx {
}
class xxx extents xx {
}
参考文献
- Martin,Lex Spoon,Bill Venners. Programming in Scala.