Scala是一门函数式的面向对象的语言,它运行在Java虚拟机上。
eg1、
示例代码:
scala>var helloWorld = "hello" + " world"
println(helloWorld)
scala>val again = " again"
helloWorld = helloWorld + again
println(helloWorld)
输出:
hello world
hello world again
eg2、定义函数 def
示例代码:
def square(a: Int) = a * a
def squareWithBlock(a: Int) = { a * a}
val squareVal = (a: Int) => a * a (这里是将Int整数a映射为a*a的函数)
def addOne(f: Int => Int, arg: Int) = f(arg) + 1(这里的=>表示所映射的类型,这就表示这个参数要是一个参数为int类型,返回的类型也是int 的方法)
println("square(2):" + square(2))
println("squareWithBlock(2):" + squareWithBlock(2))
println("squareVal(2):" + squareVal(2))
println("addOne(squareVal,2):" + addOne(squareVal, 2))
输出结果:
square(2):4
squareWithBlock(2):4
squareVal(2):4
addOne(squareVal,2):5
eg3、
代码实例:
import scala.reflect.io.File
import java.util.Scanner
def withScanner(f: File, op: Scanner => Unit) = { // 没有返回值的默认返回的是Unit
val scanner = new Scanner(f.bufferedReader)
try {
op(scanner)
} finally {
scanner.close()
}
}
withScanner(File("/proc/self/stat"),
scanner => println("pid is " + scanner.next()))
其中widthScanner封装了try-Catch块,调用者不用再close了
返回结果:
pid is 6065
eg4、定义类
class Persion(val firstName: String, val lastName: String) { //通过class定义类,val来定义字段,在类中def定义类中的函数,其中firstName和lastName会自动生成get,set方法
private var _age = 0
def age = _age //这是一个方法
def age_=(newAge: Int) = _age = newAge
def fullName() = firstName + " " + lastName
override def toString() = fullName() //这是覆盖toString方法
}
val obama: Persion = new Persion("Barack", "Obama") //用new的方式创建类
println("Persion: " + obama)
println("firstName: " + obama.firstName)
println("lastName: " + obama.lastName)
obama.age_=(51)
println("age: " + obama.age)
返回结果:
Persion: Barack Obama
firstName: Barack
lastName: Obama
age: 51
eg5、
运行实例:
def withClose(closeAble: { def close(): Unit }, //这里是将{def close():Unit}这个方法作为一种类型
op: { def close(): Unit } => Unit) {
try {
op(closeAble)
} finally {
closeAble.close()
}
}
class Connection {
def close() = println("close Connection")
}
val conn: Connection = new Connection()
withClose(conn, conn =>
println("do something with Connection"))
运行结果:
do something with Connection
close Connection
eg6、柯里化(currying)技术
def add(x:Int, y:Int) = x + y是普通的函数
def add(x:Int) = (y:Int) => x + y
是柯里化后的函数,相当于返回一个匿名函数表达式。
def add(x:Int)(y:Int) = x + y
是简化写法
def withClose(closeAble: { def close(): Unit }) //小白一枚,还是没有弄明白为什么我创建这个方法时候总是无效的???
(op: { def close(): Unit } => Unit) {
try {
op(closeAble)
} finally {
closeAble.close()
}
}
class Connection {
def close() = println("close Connection")
}
val conn: Connection = new Connection()
withClose(conn)(conn =>
println("do something with Connection"))
eg7、泛型
def withClose[A <: { def close(): Unit }, B](closeAble: A)//这里的A表示的是{def close():Unit}的子类型
(f: A => B): B =
try {
f(closeAble)
} finally {
closeAble.close()
}
class Connection {
def close() = println("close Connection")
}
val conn: Connection = new Connection()
val msg = withClose(conn) { conn =>
{
println("do something with Connection")
"123456"
}
}
println(msg)
返回结果:
do something with Connection
close Connection
123456
eg8 Traits 相当于java中的接口implements
trait ForEachAble[A] {
def iterator: java.util.Iterator[A]
def foreach(f: A => Unit) = {
val iter = iterator
while (iter.hasNext)
f(iter.next)
}
}
trait JsonAble {
def toJson() =
scala.util.parsing.json.JSONFormat.defaultFormatter(this)
}
val list = new java.util.ArrayList[Int]() with ForEachAble[Int] width JsonAble
list.add(1); list.add(2)
println("For each: "); list.foreach(x => println(x))
//println("Json: " + list.toJson())
Scala调用url获取返回值 http://www.linuxidc.com/Linux/2016-12/138517.htm
Scala配置Intellij IDEA 15.0.3环境及hello world! http://www.linuxidc.com/Linux/2016-08/134140.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140823.htm
