Scala实例分析
发布时间:2021-12-09 20:22:36 所属栏目:教程 来源:互联网
导读:Scala是一门函数式的面向对象的语言,它运行在Java虚拟机上。 eg1、 示例代码: scalavar helloWorld = hello + world println(helloWorld) scalaval again = again helloWorld = helloWorld + again println(helloWorld) 输出: hello world hello world ag
|
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()) ![]() (编辑:我爱制作网_潮州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |



浙公网安备 33038102330565号