package com.parllay.scala.type_parameterizitor
import scala.io.{Source, BufferedSource}
/**
* Created by richard on 15-8-17.
* 第58讲:Scala中Abstract Types实战详解
*/
/**
* scala里的类型,除了在定义class,trait,object时会产生类型,
* 还可以通过type关键字来声明类型。
type相当于声明一个类型别名:
scala> type S = String
defined type alias S
上面把String类型用S代替,通常type用于声明某种复杂类型,或用于定义一个抽象类型。
场景1 用于声明一些复杂类型,比如下面声明一个结构类型
scala> type T = Serializable {
| type X
| def foo():Unit
| }
defined type alias T
这个结构类型内部也允许通过type来定义类型,这里对X没有赋值表示X是一个抽象类型,
需要子类型在实现时提供X具体的类型。下面是一个T类型的具体实例:
scala> object A extends Serializable{ type X=String; def foo(){} }
scala> typeOf[A.type] <:< typeOf[T]
res19: Boolean = true
场景2 用于抽象类型
scala> trait A { type T ; def foo(i:T) = print(i) }
scala> class B extends A { type T = Int }
scala> val b = new B
scala> b.foo(200)
200
scala> class C extends A { type T = String }
scala> val c = new C
scala> c.foo("hello")
hello
*/
trait Reader{
type In <: java.io.Serializable
type Contents
def read(in: In): Contents
}
class FileReader extends Reader {
type In = String
type Contents = BufferedSource
override def read(name : In) = Source.fromFile(name)
}
object Abstract_Type {
def main(args: Array[String]) {
val fileReader = new FileReader
val content = fileReader.read("path")
}
}
王家林亲授《DT大数据梦工厂》大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频、PPT、代码下载:
百度云盘:http://pan.baidu.com/s/1c0noOt6
腾讯微云:http://url.cn/TnGbdC
360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2
土豆:http://www.tudou.com/programs/view/E5QGmPvfupc/
优酷:http://v.youku.com/v_show/id_XMTI4ODEwMzM3Mg==.html?from=s1.8-1-1.2
爱奇艺:http://www.iqiyi.com/w_19rrt570q9.html#vfrm=2-3-0-1
腾讯视频:http://v.qq.com/boke/page/i/0/v/i0159zikiqv.html
技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群
DT大数据梦工厂① :462923555
DT大数据梦工厂②:437123764
DT大数据梦工厂③ :418110145
微信公众账号: DT_Spark
王家林老师微信号: 18610086859
王家林老师QQ: 1740415547
王家林老师邮箱: 18610086859@126.com
本视频由王家林老师, 亲自讲解, 完全通过代码实战把您带人大数据的时代.
原文:http://www.cnblogs.com/czh-liyu/p/4735472.html