scala学习笔记(十五) 高等类型
2015-09-21 14:54
141 查看
类或特质可以定义一个在子类中被具体化的抽象类型,如下:
trait Reader {
type Contents //这里定义一个抽象类型,子类中必须指定这个类型
def read(fileName: String): Contents
}
class StringReader extends Reader {
type Contents = String
def read(fileName: String) =Source.fromFile(fileName, "UTF-8").mkString
}
class ImageReader extends Reader {
type Contents = BufferedImage
def read(fileName: String) =ImageIO.read(new File(fileName))
}
//上面的抽象类型也可以写为下面的这种形式
这种方式是 类型参数
trait ReaderC[C] {
def read(fileName: String): C
}
抽象类型还可以使用界定,与类型参数一样
trait Listener {
type Event <: java.util.EventObject
}
这样在子类中指定的时候EVENT必须是EventObject的一个兼容的类型
package demo
/**
* @author Administrator
*/
object ScalaType {
object show
object then
object around
def main(args: Array[String]): Unit = {
// val bug = new Bug()
// bug move 4 and show and then move 6 and show turn around move 5 and show
printValues((x: Int) => x * x, 3, 6)
printValues(Array(1, 1, 2, 3, 5, 8, 13, 21,34, 55), 3, 6)
}
class Bug {
var direction: Boolean = true
var postion: Int = 0
def move(step: Int) = {
val tmp = if (direction) step else -step
postion += tmp
this
}
def turn(): this.type = {
direction = !direction
this
}
// def show = {
// print(postion + " ")
// this
// }
def and(obj: show.type) = { print(postion +" "); this }
def and(obj: then.type) = this
def turn(obj: around.type): this.type =turn()
}
def fun(obj: { def close(): Unit }, f: { defclose(): Unit } => Unit) {
try {
f(obj)
} catch {
case e: Exception => obj.close();e.printStackTrace()
} finally {
obj.close()
}
}
def printValues(f: { def apply(v: Int): Int}, from: Int, to: Int) {
for (i <- from to to)println(f.apply(i))
}
abstract class Dim[T](val value: Double, valname: String) {
this: T
4000
=>
protected def create(v: Double): T
def +(other: Dim[T]) = create(value +other.value)
override def toString() = value + "" + name
}
class Seconds(v: Double) extendsDim[Seconds](v, "s") {
override def create(v: Double) = newSeconds(v)
}
class Meters(v: Double) extendsDim[Meters](v, "m") {
override def create(v: Double) = newMeters(v)
}
}
trait Reader {
type Contents //这里定义一个抽象类型,子类中必须指定这个类型
def read(fileName: String): Contents
}
class StringReader extends Reader {
type Contents = String
def read(fileName: String) =Source.fromFile(fileName, "UTF-8").mkString
}
class ImageReader extends Reader {
type Contents = BufferedImage
def read(fileName: String) =ImageIO.read(new File(fileName))
}
//上面的抽象类型也可以写为下面的这种形式
这种方式是 类型参数
trait ReaderC[C] {
def read(fileName: String): C
}
抽象类型还可以使用界定,与类型参数一样
trait Listener {
type Event <: java.util.EventObject
}
这样在子类中指定的时候EVENT必须是EventObject的一个兼容的类型
package demo
/**
* @author Administrator
*/
object ScalaType {
object show
object then
object around
def main(args: Array[String]): Unit = {
// val bug = new Bug()
// bug move 4 and show and then move 6 and show turn around move 5 and show
printValues((x: Int) => x * x, 3, 6)
printValues(Array(1, 1, 2, 3, 5, 8, 13, 21,34, 55), 3, 6)
}
class Bug {
var direction: Boolean = true
var postion: Int = 0
def move(step: Int) = {
val tmp = if (direction) step else -step
postion += tmp
this
}
def turn(): this.type = {
direction = !direction
this
}
// def show = {
// print(postion + " ")
// this
// }
def and(obj: show.type) = { print(postion +" "); this }
def and(obj: then.type) = this
def turn(obj: around.type): this.type =turn()
}
def fun(obj: { def close(): Unit }, f: { defclose(): Unit } => Unit) {
try {
f(obj)
} catch {
case e: Exception => obj.close();e.printStackTrace()
} finally {
obj.close()
}
}
def printValues(f: { def apply(v: Int): Int}, from: Int, to: Int) {
for (i <- from to to)println(f.apply(i))
}
abstract class Dim[T](val value: Double, valname: String) {
this: T
4000
=>
protected def create(v: Double): T
def +(other: Dim[T]) = create(value +other.value)
override def toString() = value + "" + name
}
class Seconds(v: Double) extendsDim[Seconds](v, "s") {
override def create(v: Double) = newSeconds(v)
}
class Meters(v: Double) extendsDim[Meters](v, "m") {
override def create(v: Double) = newMeters(v)
}
}
相关文章推荐
- Windows下Scala环境搭建
- Windows7下安装Scala 2.9.2教程
- XML 文件解析--含Unicode字符的XML文件
- 分分钟掌握快速排序(Java / Scala 实现)
- Scala极速入门
- Spark初探
- Scala实现REST操作
- Scala method call syntax
- 关于Scala多重继承的菱形问题
- Scala 高阶函数(high-order function)剖析
- Spray.io搭建Rest服务
- Spray.io搭建Rest — 支持Twirl模板并部署
- 搭建hadoop/spark集群环境
- zeppelin入门使用
- ScalaMP ---- 模仿 OpenMp 的一个简单并行计算框架
- 用Scala实现延迟计算
- SBT学习 [持续更新...]
- Scala创建新的控制结构
- Scala: 一次命令式到函数式的重构
- 浅谈Scala的特质(trait)