设为首页 加入收藏

TOP

Scala实例详解(一)
2017-02-21 08:16:02 】 浏览:356
Tags:Scala 实例 详解

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 squareva l = (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("squareva l(2):" + squareva l(2))
println("addOne(squareva l,2):" + addOne(squareva l, 2))
输出结果:
square(2):4
squareWithBlock(2):4
squareva l(2):4
addOne(squareva l,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(

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇enc28j60网卡驱动模块添加进Linux.. 下一篇Mybatis实战之自定义TypeHandler..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目