Scala 深入浅出实战经典 第54讲:Scala中复合类型实战详解

王家林亲授《DT大数据梦工厂》大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频、PPT、代码下载:
百度云盘:http://pan.baidu.com/s/1c0noOt6
腾讯微云:http://url.cn/TnGbdC
360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2
土豆:http://www.tudou.com/programs/view/a6qIB7SqOlc/
优酷:http://v.youku.com/v_show/id_XMTI4NzM2NjM0MA==.html?from=s1.8-1-1.2
爱奇艺:http://www.iqiyi.com/w_19rrtik9eh.html#vfrm=2-3-0-1
腾讯视频: http://v.qq.com/boke/page/c/0/3/c01594nmz73.html
技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群

DT大数据梦工厂① :462923555
DT大数据梦工厂②:437123764
DT大数据梦工厂③ :418110145

微信公众账号: DT_Spark
王家林老师微信号: 18610086859
王家林老师QQ: 1740415547
王家林老师邮箱: 18610086859@126.com

本视频由王家林老师, 亲自讲解, 完全通过代码实战把您带人大数据的时代.

 

package com.parllay.scala.type_parameterizitor

/**
 * Created by richard on 15-8-9.
 * 第54讲:Scala中复合类型实战详解
 */
trait Compound_Type1; trait Compound_Type2;
class Compound_Type extends Compound_Type1 with Compound_Type2

object Compound_Type {

  def compound_Type(x: Compound_Type1 with Compound_Type2) = {println("Compound Type in global method")}

  def main(args: Array[String]) {

    /**
     *  new 一个复合类型
     */
    compound_Type(new Compound_Type with Compound_Type1 with Compound_Type2)

    /**
     * 创建一个object
     */
    object Compound_Type_Object extends Compound_Type1 with Compound_Type2

    compound_Type(Compound_Type_Object)

    /**
     * 利用type申明别名
     */
    type compound_Type_Alias = Compound_Type1 with Compound_Type2

    def compound_Type_Local(x: compound_Type_Alias) = println("Compound Type in local method")

    /**
     *
     */
    val compound_Type_Class = new Compound_Type

    compound_Type_Local(compound_Type_Class)

    /**
     * 复合类型中也可以包含结构类型
     */
    type Scala = Compound_Type1 with Compound_Type2 {def init(): Unit}

  }

}

/**
 * class A extends B with C with D with E
应做类似如下形式解读:

class A extends (B with C with D with E)

T1 with T2 with T3 …
这种形式的类型称为复合类型(compound type)或者也叫交集类型(intersection type)。

跟结构类型类似,可以在一个方法里声明类型参数时使用复合类型:

scala> trait X1; trait X2;

scala> def test(x: X1 with X2) = {println("ok")}
test: (x: X1 with X2)Unit

scala> test(new X1 with X2)
ok

scala> object A extends X1 with X2

scala> test(A)
ok
也可以通过 type 声明:

scala> type X = X1 with X2
defined type alias X

scala> def test(x:X) = println("OK")
test: (x: X)Unit

scala> class A extends X1 with X2

scala> val a = new A

scala> test(a)
OK
在上一篇介绍结构类型时也提到过复合类型中也可包含结构类型:

scala> type X = X1 with X2 { def close():Unit }
defined type alias X
 */

  

posted @ 2015-08-11 21:28  曹振华  阅读(382)  评论(0编辑  收藏  举报