【问题标题】:Scala Mixin with self=> traits具有 self=> 特征的 Scala Mixin
【发布时间】:2017-07-14 06:56:28
【问题描述】:

假设我有一个基本特征,而我的特征正在扩展基本特征。我想创建一个具有混合 self=> 特征的类,并且可以动态调用重载。

【问题讨论】:

    标签: scala traits


    【解决方案1】:

    以下是解决方案

    package minxin.neel.test
    
        /**
          * Created by Neelamadhab.
         */
        class TestMix2 {
          def hello = println("Hello NEEL...")
        }
    
       class BaseMixInClass {
          self: BaseTrait =>
          self.method
       }
    
       trait BaseTrait {
          def method = println("Inside method in BaseTrait")
       } 
    
       trait Trait2 extends BaseTrait {
          override def method = println("Hello, Inside method in Trait2")
       }
    
    trait Trait3 extends BaseTrait {
      override def method = println("Hello, Inside method in Trait3")
    }
    
    trait Trait4 extends BaseTrait
    
    
    object MyMixinTest extends App {
      new BaseMixInClass with Trait2
      new BaseMixInClass with Trait3
      new BaseMixInClass with Trait4
      new BaseMixInClass with BaseTrait
    }
    

    输出将是:

    Hello, Inside method in Trait2
    Hello, Inside method in Trait3
    Inside method in BaseTrait
    Inside method in BaseTrait
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2012-10-22
      • 2017-07-05
      相关资源
      最近更新 更多