Scala深入浅出进阶经典 第53讲:Scala中结构类型实战详解

来源:互联网 发布:从哪里查淘宝消费总额 编辑:程序博客网 时间:2024/05/21 17:52
package com.dt.scalaInAction.demo_053/** * Scala中结构类型实战详解 *  * 结构类型不关心传入的类型 只关心传入的对象具有某一种行为 */class Structural {    def open() = println("A class instance Opened")}object Structural_Type {    def main(args: Array[String]): Unit = {        init(new { def open()=println("Opened")})                /*         * type关键字的作用是把"="右边的内容取一个别名           */        type X = {def open():Unit}        def init(res: X) = res.open        init(new {def open(){println("Opened again")}})                /**         * 定义一个单例对象         */        object A {            def open(){                println("A single object Opened")            }        }        init(A)                val structual = new Structural        init(structual)            }        /**     * 注意:这个函数的定义来看, 并不关心传入对象类型是什么,只关心传入对象必须具有open方法即可     */    def init(res: {def open():Unit}) {    res.open()    }    }


以上内容是从王家林老师DT大数据课程第53讲的学习笔记和个人整理。
DT大数据微信公众账号:DT_Spark
王家林老师QQ:1740415547
王家林老师微信号:18610086859
第53讲视频网站地址:http://pan.baidu.com/s/1ntEGt4X


0 0
原创粉丝点击