zoukankan      html  css  js  c++  java
  • Scala编程入门---面向对象编程之Trait高级知识

    trait调用链

    Scala中支持让类继承多个Trait后,依次调用多个Trait中的同一个方法,只要让多个trait的同一个方法中,在最后都执行super.方法即可

    类中调用多个trait中都有这个方法时,首先会从最右边的trait的方法开始执行,然后依次往左执行,形成一个调用链条

    这种特性非常强大,其实就相当于设计模式中的责任链模式的一种具体实现依赖

    trait Handle{
        def handle(data:String){}
    }
    
    trait DataValidHandler extends Handler{
        override def  handle(data:String){
           println("check data :"+data)
            super.handle(data)
        }
    }
    
    tarit SignatureValidHander extends Handler{
        override def handle(data:String){
           println("check singature:"+data)
            super.handle(data)
        }
    }
    
    class Person(val name:String) extendsSignatuerValidHander with DataValidHandler{
        def sayHello={ print("Hello,"+name);handle(name)}
    }
    
    
    val p = new Person("leo")
    p.sayHello
    
    #结果是:
    hello,leo
    check data :leo
    check singature :leo

    在trait中覆盖抽象方法

    在trait中,是可以覆盖父trait的抽象方法的

    但是覆盖时,如果使用了super.方法的代码,则无法通过编译。因为super.方法就会去掉用父trait的抽象方法,此时子trait的该方法还是会被认为是抽象的。

    此时如果要通过编译,就得给子trait的方法加上abstract override修饰

    trait Logger{
    
        def log(msg:String)
    
    }
    
    trait MyLogger extends Logger{
            abstract override def log(msg:String){super.log(msg))
    }

    混合使用trait的具体方法和抽象方法

    在trait中,可以混合使用具体方法和抽象方法

    可以让具体方法依赖于抽象方法,而抽象方法则放到继承trait的类中去实现

    这种trait其实就是设计模式中的模板设计模式的体现

    #抽象方法的具体实现和具体实现的调用
    trait Valid{ def getName name:String def valid
    = Boolean{ getName ="leo" } } class Pserson(name:String) extends Valid{ print(valid) getName=name }

    trait的构造机制

    在Scala中,trait也是有构造代码的,也就是trait中的,不包含在任何方法中的代码

    而继承trait的类的构造机制如下:1、父类的构造函数执行;2、trait的改造代码执行,多个trait从左往右依次执行;3、构造trait,如果多个trait继承同一个父trait,则父trait只会构造一次;4、所有trait构造完毕之后,子类的构造函数执行

    class Person{ print("person's constractor!")}
    trait Logger{print("Logger's constractor!")}
    trait MyLogger extends Logger{print("MyLogger's constractor!")}
    trait TimeLogger extends Logger{print("TimeLogger's constractor!")}
    class Student extends Person with MyLogger with TimeLogger{print("student's constractor!")}
    
    val s = new Strudent
    
    #结果:
    
    person's constractor!
    Logger's constractor!
    MyLogger's constractor!
    TimeLogger's constractor!    
    student's constractor!
  • 相关阅读:
    每日学习心得:找不到类型{0},它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素...中提供,cellspacing、rowspacing属性
    我所理解的设计模式(C++实现)——解释器模式(Interpreter Pattern)
    uva-112 Tree Summing
    带你走进EJB--它都有哪些Bean
    Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力
    Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块
    《郭大侠的妹妹不可能那么可爱》 第一章 郭大侠那可爱的妹妹?
    Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs
    Codeforces Round #356 (Div. 2) C. Bear and Prime 100 水题
    Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
  • 原文地址:https://www.cnblogs.com/yeszero/p/6957018.html
Copyright © 2011-2022 走看看