<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                任何類只能`extends`一個父類,但可以混入多個特質,混入用關鍵字`with`實現,相當于Java中用`implements`關鍵字實現接口一樣。 ```scala // 1. 在聲明類的時候混入特質 class A extedns B with TraitC with TraitD with ... // 2. 在new對象時混入特質 val A = new A with TraitC with TraitD with ... ``` <br/> **1. 混入特質** ```scala // 抽象類 abstract class Animal { val message:String = "abstract class Animal" } // 類 class Dog extends Animal { override val message: String = "class Dog" } // 特質 trait Mammal extends Animal { override val message: String = "trait Mammal" } // 特質 trait Pet extends Animal { override val message: String = "trait Pet" } // 混入特質 class Twoha extends Dog with Mammal with Pet object App { def main(args: Array[String]): Unit = { val twoHa = new Twoha println(twoHa.message) // trait Pet } } ``` * 當混入有多個同名的成員時,調用的是最右邊的成員。如上面輸出的`trait Pet`為最右邊的`message`,當然如果子類重寫了該成員,調用的就是子類的了。 **2. 混入特質的構造順序** 構造順序由左往右,如果前面已經有某個父類被構造一次,則后面不再重新構造,比如上面的Animal類雖然被多個子類繼承,但從左到右只被構造一次。 具體構造順序從左往右如下: ```scala 超類構造器 -> 父特質構造器 -> 子特質構造器 -> 子類構造器 所以 class Twoha extends Dog with Mammal with Pet 的構造順序如下: Animal -> Dog -> Mammal -> Pet -> Twoha ``` <br/> **3. 特質對 `super`的動態調用** ```scala class Root { def hello() { println("Hello, Root!")} } class SubA extends Root { override def hello(){ super.hello() // 在類中super調用為靜態調用 println("Hello, SubA!") } } trait SubB extends Root { override def hello(){ super.hello(); // 在特質中super調用是動態調用 println("Hello, SubB") } } ``` 在 SubA 中,`super` 的調用是靜態綁定的,父類 Root 的 hello() 將被調用。而在 SubB 中,`super` 的調用是動態綁定的,即在定義特質 SubB 的時候,`super` 還不確定,直到特質被混入到具體類的時候才確定。 ```scala object HelloWorld{ def main(args: Array[String]): Unit = { val a = new Root with SubB // SubB被混入到Root中,則super為Root類, super.hello()為Root中的hello() a.hello() // Hello, Root! // Hello, SubB! val b = new SubA with SubB // SubB被混入到SubA中,則super為SubA類, super.hello()為SubA中的hello() // 調用是SubA中的hello() b.hello() // Hello, Root! // Hello, SubA! // Hello, SubB! } } ``` <br/> **4. 特質線性化** 線性化給了在多個特質中 `super` 被解析的順序。 ```scala class Animal trait Furry extends Animal trait HasLegs extends Animal trait FourLegged extends HasLegs class Cat extends Animal with Furry with FourLegged ``` 類 Cat 的繼承層級和線性化次序展示在下圖。 繼承次序使用傳統的 UML 標注指明:白色箭頭表明繼承,箭頭指向超類型。黑色箭頭說明線性化次序,箭頭指向 `super` 調用解決的方向。 ![](https://img.kancloud.cn/3f/22/3f22f49af22a2a85f5bb078e9d9cb805_1155x425.png) Cat 的線性化次序為:Cat >> FourLegged >> HasLegs >> Furry >> Animal `>>`意思是:串接并去掉重復項, 右側勝出,如下 ```scala class Cat extends Animal with Furry with FourLegged lin(Cat) = Cat >> lin(FourLegged) >> lin(Furry) >> lin(Animal) = Cat >> (FourLegged >> HasLegs) >> (Furry >> Animal) >> (Animal) = Cat >> FourLegged >> HasLegs >> Furry >> Animal ``` <br/> **5. 特質調用鏈** 特質調用鏈就是子特質重寫父特質的方法,然后在該方法中使用`super`關鍵字調用父特質的方法。 ```scala trait TraitA { def print = println("traitA") } trait TraitB extends TraitA { override def print: Unit = { super.print // 使用super調用父類的print函數 println("traitB") } } trait TraitC extends TraitB { override def print: Unit = { super.print println("traitC") } } class ClassD {} object App { def main(args: Array[String]): Unit = { val classD = new ClassD with TraitC with TraitB with TraitA classD.print } } ==========Output========== traitA traitB traitC ``` 可以看到 print 方法<mark>從右往左</mark>開始被調用,形成一個調用鏈。 總結:越靠近后面的特質越優先起作用,當調用帶混入的類的方法時,最右側特質的方法首先被調用。如果哪個方法調用了 `super`,則它調用其左側特質的方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看